Exemplo n.º 1
0
Arquivo: msg.go Projeto: jwk000/tabtoy
// 添加消息数组
func (self *DynamicMessage) AddRepeatedMessage(fd *pbmeta.FieldDescriptor, value *DynamicMessage) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is not message: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	fv := self.fetchValue(fd, true)

	if !fd.IsRepeated() {
		return false
	}

	if fv.msgArray == nil {
		fv.msgArray = make([]*DynamicMessage, 0)
	}

	fv.msgArray = append(fv.msgArray, value)

	return true
}
Exemplo n.º 2
0
Arquivo: lua.go Projeto: davyxu/tabtoy
func (self *luaWriter) RepeatedMessageBegin(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, msgCount int, indent int) {

	if indent == 1 {
		self.printer.WriteString(fmt.Sprintf("%s = {\n", fd.Name()))
	} else {
		self.printer.WriteString(fmt.Sprintf("%s = {", fd.Name()))
	}
}
Exemplo n.º 3
0
// 普通值
func (self *jsonWriter) WriteValue(fd *pbmeta.FieldDescriptor, value string, indent int) {

	if fd.IsRepeated() {
		self.printer.WriteString(jsonvalueWrapper(fd, value))
	} else {
		self.printer.WriteString(fmt.Sprintf("\"%s\": %s", fd.Name(), jsonvalueWrapper(fd, value)))
	}

}
Exemplo n.º 4
0
Arquivo: msg.go Projeto: jwk000/tabtoy
// 设置单一值
func (self *DynamicMessage) SetValue(fd *pbmeta.FieldDescriptor, value string) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is message not value: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	fv := self.fetchValue(fd, false)

	if fd.IsRepeated() {

		return false

	}

	// 可选字段
	if fd.IsOptional() {

		var existValue string
		if fv != nil {
			existValue = fv.value
		}

		// 有指派默认值
		if fd.DefaultValue() != "" {
			existValue = fd.DefaultValue()
		} else {
			// 没有指派默认值, 取值的默认值
			existValue = GetDefaultValue(fd)
		}

		// 输入值和已经存在的值一致, 就无需设置了
		if existValue == value {
			return true
		}

	}

	if fv == nil {
		fv = self.addValue(fd)
	}

	fv.value = value

	return true
}
Exemplo n.º 5
0
Arquivo: lua.go Projeto: davyxu/tabtoy
// Value是消息的字段
func (self *luaWriter) WriteMessage(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, indent int) {

	if indent == 1 || fd.IsRepeated() {
		self.printer.WriteString("{")

	} else {
		self.printer.WriteString(fmt.Sprintf("%s = {", fd.Name()))
	}

	rawWriteMessage(&self.printer, self, msg, indent)

	self.printer.WriteString("}")

}
Exemplo n.º 6
0
// 重复值检查, 如果meta中描述有开启则开启
func (self RepeatValueChecker) Check(meta *tool.FieldMeta, fd *pbmeta.FieldDescriptor, value string) {

	if meta == nil || meta.RepeatCheck == false {
		return
	}

	if self.contain(value) {
		log.Errorf("detected duplicate value %s=%s", fd.Name(), value)
		return
	}

	self[value] = true

}
Exemplo n.º 7
0
Arquivo: pbt.go Projeto: davyxu/tabtoy
// Value是消息的字段
func (self *pbtWriter) WriteMessage(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, indent int) {

	pos := self.printer.Len()

	self.printer.WriteString(fmt.Sprintf("%s {", fd.Name()))

	valueWrite := rawWriteMessage(&self.printer, self, msg, indent)

	self.printer.WriteString("}")

	// 如果没写入值, 将回滚写入
	if valueWrite == 0 {
		self.printer.Truncate(pos)
	}
}
Exemplo n.º 8
0
Arquivo: lua.go Projeto: davyxu/tabtoy
// 普通值
func (self *luaWriter) WriteValue(fd *pbmeta.FieldDescriptor, value string, indent int) {

	var finalValue string
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING,
		pbprotos.FieldDescriptorProto_TYPE_ENUM:
		finalValue = util.StringEscape(value)
	case pbprotos.FieldDescriptorProto_TYPE_INT64,
		pbprotos.FieldDescriptorProto_TYPE_UINT64:
		finalValue = fmt.Sprintf("\"%s\"", value)
	default:
		finalValue = value
	}

	self.printer.WriteString(fmt.Sprintf("%s = %s", fd.Name(), finalValue))
}
Exemplo n.º 9
0
Arquivo: msg.go Projeto: jwk000/tabtoy
// 单一消息
func (self *DynamicMessage) SetMessage(fd *pbmeta.FieldDescriptor, value *DynamicMessage) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is not message: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	self.fetchValue(fd, true).msg = value

	return true
}
Exemplo n.º 10
0
// Value是消息的字段
func (self *jsonWriter) WriteMessage(fd *pbmeta.FieldDescriptor, msg *data.DynamicMessage, indent int) {

	pos := self.printer.Len()

	if indent == 1 || fd.IsRepeated() {
		self.printer.WriteString("{")

	} else {
		self.printer.WriteString(fmt.Sprintf("\"%s\" : {", fd.Name()))
	}

	valueWrite := rawWriteMessage(&self.printer, self, msg, indent)

	self.printer.WriteString("}")

	// 如果没写入值, 将回滚写入
	if valueWrite == 0 {
		self.printer.Truncate(pos)
	}
}
Exemplo n.º 11
0
Arquivo: msg.go Projeto: davyxu/tabtoy
// 添加值数组
func (self *DynamicMessage) AddRepeatedValue(fd *pbmeta.FieldDescriptor, value string) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is message not value: '%s' in '%s', value: '%s'", fd.Name(), self.Desc.Name(), value)
		return false
	}

	fv := self.fetchValue(fd, true)

	if !fd.IsRepeated() {

		log.Errorf("field is non-repeated by used like repeated value", fd.Name())

		return false
	}

	if fv.valueArray == nil {
		fv.valueArray = make([]string, 0)
	}

	fv.valueArray = append(fv.valueArray, value)

	return true
}
Exemplo n.º 12
0
Arquivo: msg.go Projeto: davyxu/tabtoy
// 删除值
func (self *DynamicMessage) ClearFieldValue(fd *pbmeta.FieldDescriptor) bool {

	if fd == nil || !self.Desc.Contains(fd) {
		log.Errorf("field not found: '%s' in '%s' ", fd.Name(), self.Desc.Name())
		return false
	}

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		log.Errorf("field is message not value: '%s' in '%s'", fd.Name(), self.Desc.Name())
		return false
	}

	fv := self.fetchValue(fd, false)

	if fv == nil {
		return true
	}

	delete(self.fieldMap, fd)

	return true
}
Exemplo n.º 13
0
func printField(gen *Generator, fd *pbmeta.FieldDescriptor, msg *pbmeta.Descriptor, file *pbmeta.FileDescriptor) {
	typeStr := fieldTypeString(fd)
	memberVar := "_" + fd.Name()

	//  private int _Age = default(int);

	if fd.IsRepeated() {
		gen.Println("private readonly ", typeStr, " ", memberVar, " = new ", typeStr, "();")

	} else {

		gen.Println("private ", typeStr, " ", memberVar, " = ", getDefaultValue(fd), ";")

	}

	// [global::ProtoBuf.ProtoMember(10, IsRequired = false, Name=@"Age", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
	gen.BeginLine()

	gen.Print("[global::ProtoBuf.ProtoMember(", fd.Define.GetNumber())

	if !fd.IsRepeated() {
		gen.Print(", IsRequired = ", isRequired(fd))
	}

	gen.Print(", Name=@\"", fd.Name(), "\", DataFormat = global::ProtoBuf.DataFormat.", getDataFormat(fd), ")]")

	gen.EndLine()

	if !fd.IsRepeated() {
		gen.Println("[global::System.ComponentModel.DefaultValue(", getDefaultValue(fd), ")]")
	}

	// 属性定义
	gen.Println("public ", typeStr, " ", fd.Name())
	gen.Println("{")
	gen.In()
	gen.Println("get { return ", memberVar, "; }")

	if !fd.IsRepeated() {
		gen.Println("set { ", memberVar, " = value; }")
	}

	gen.Out()
	gen.Println("}")

}
Exemplo n.º 14
0
func (self *jsonWriter) RepeatedValueBegin(fd *pbmeta.FieldDescriptor) {
	self.printer.WriteString(fmt.Sprintf("\"%s\" : [", fd.Name()))
}
Exemplo n.º 15
0
func Value2Struct(meta *tool.FieldMetaV1, structValue string, fd *pbmeta.FieldDescriptor, callback func(string, string) bool) (isValue2Struct bool, hasError bool) {

	if meta == nil {
		return
	}

	if meta.String2Struct == false {
		return
	}

	if !fd.IsMessageType() {
		hasError = true
		log.Errorf("%s is not message type", fd.Name())
		return
	}

	msgD := fd.MessageDesc()

	if msgD == nil {
		hasError = true
		log.Errorf("%s message not found", fd.Name())
		return
	}

	p := newLineParser(fd, structValue)

	// 匹配顺序从高到低

	defer func() {

		err := recover()

		switch err.(type) {
		// 运行时错误
		case interface {
			RuntimeError()
		}:
			// 继续外抛, 方便调试
			panic(err)
		case error:
			hasError = true
			log.Errorf("field: %s parse error, %v", fd.Name(), err)

		default:
			isValue2Struct = true
		}

	}()

	p.NextToken()

	for {

		if p.TokenID() != Token_Identifier {
			hasError = true
			log.Errorf("expect key in field: %s", fd.Name())
			return
		}

		key := p.TokenValue()

		structFD := FieldByNameWithMeta(msgD, key)

		// 尝试查找字段定义
		if structFD == nil {

			hasError = true
			log.Errorf("%s field not found ", key)
			return
		}

		p.NextToken()

		if p.TokenID() != Token_Comma {
			hasError = true
			log.Errorf("%s need ':' split value", key)
			return
		}

		p.NextToken()

		value := p.TokenValue()

		// 按照正常流程转换值
		if afterValue, ok := ValueConvetor(structFD, value); ok {

			if !callback(key, afterValue) {
				hasError = true
				return
			}

		} else {
			hasError = true
			log.Errorf("%s convert failed", key)
			return
		}

		p.NextToken()

	}

	return
}
Exemplo n.º 16
0
Arquivo: pbt.go Projeto: davyxu/tabtoy
// 普通值
func (self *pbtWriter) WriteValue(fd *pbmeta.FieldDescriptor, value string, indent int) {

	self.printer.WriteString(fmt.Sprintf("%s: %s", fd.Name(), valueWrapper(fd, value)))
}
Exemplo n.º 17
0
func ValueConvetor(fd *pbmeta.FieldDescriptor, value string) (string, bool) {

	// 空单元格时, 给定一个这个类型对应的值
	if value == "" {
		value = data.GetDefaultValue(fd)
	}

	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT:

		_, err := strconv.ParseFloat(value, 32)

		if err != nil {

			return "", false
		}

	case pbprotos.FieldDescriptorProto_TYPE_INT64:

		_, err := strconv.ParseInt(value, 10, 64)
		if err != nil {

			return "", false
		}

	case pbprotos.FieldDescriptorProto_TYPE_UINT64:

		_, err := strconv.ParseUint(value, 10, 64)
		if err != nil {
			return "", false
		}

	case pbprotos.FieldDescriptorProto_TYPE_INT32:

		_, err := strconv.ParseInt(value, 10, 32)
		if err != nil {
			return "", false
		}

	case pbprotos.FieldDescriptorProto_TYPE_UINT32:

		_, err := strconv.ParseUint(value, 10, 32)

		if err != nil {
			return "", false
		}

	case pbprotos.FieldDescriptorProto_TYPE_BOOL:

		var final string

		for {
			if value == "是" {
				final = "true"
				break
			} else if value == "否" {
				final = "false"
				break
			}

			v, err := strconv.ParseBool(value)

			if err != nil {
				return "", false
			}

			if v {
				final = "true"
			} else {
				final = "false"
			}

			break
		}

		value = final

	case pbprotos.FieldDescriptorProto_TYPE_ENUM:
		ed := fd.EnumDesc()

		if ed.ValueCount() == 0 {
			return "", false
		}

		// 枚举值从表格读出时可能是中文枚举值, 需要根据meta信息转换为程序枚举值
		var convValue string = value

		// 遍历这个枚举类型
		for i := 0; i < ed.ValueCount(); i++ {

			evd := ed.Value(i)

			// 取出每个字段的meta
			meta := data.GetFieldMeta(evd)

			if meta == nil {
				continue
			}

			// 这个枚举值的别名是否与给定的一致
			if meta.Alias == value {
				convValue = evd.Name()
				break
			}
		}

		if ed.ValueByName(convValue) == nil {
			log.Errorf("enum doesn't contain this value, %s in %s", convValue, fd.Name())
			return "", false
		}

		value = convValue

	}

	return value, true
}
Exemplo n.º 18
0
Arquivo: pbt.go Projeto: jwk000/tabtoy
func makeValue(fd *pbmeta.FieldDescriptor, value string) string {
	return fmt.Sprintf("%s: %s", fd.Name(), valueWrapper(fd, value))
}