Exemple #1
0
// 添加消息数组
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() {

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

		return false
	}

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

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

	return true
}
Exemple #2
0
func isRequired(fd *pbmeta.FieldDescriptor) string {
	if fd.IsRequired() {
		return "true"
	}

	return "false"
}
Exemple #3
0
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()))
	}
}
Exemple #4
0
func valueWrapper(fd *pbmeta.FieldDescriptor, value string) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		return util.StringEscape(value)
	}

	return value
}
Exemple #5
0
func wrapDefaultValue(fd *pbmeta.FieldDescriptor, typestr string) string {
	v := strings.TrimSpace(fd.DefaultValue())
	if v != "" {
		return fmt.Sprintf("(%s)%s", typestr, v)
	}

	return fmt.Sprintf("default(%s)", typestr)
}
Exemple #6
0
func valueWrapper(fd *pbmeta.FieldDescriptor, value string) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		return fmt.Sprintf("\"%s\"", value)
	}

	return value
}
Exemple #7
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)))
	}

}
func getDataFormat(fd *pbmeta.FieldDescriptor) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING,
		pbprotos.FieldDescriptorProto_TYPE_MESSAGE,
		pbprotos.FieldDescriptorProto_TYPE_BOOL:
		return "Default"
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT:
		return "FixedSize"
	}

	return "TwosComplement"
}
Exemple #9
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

}
Exemple #10
0
// 添加值数组
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() {
		return false
	}

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

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

	return true
}
Exemple #11
0
// 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("}")

}
Exemple #12
0
// 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)
	}
}
Exemple #13
0
// 普通值
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))
}
Exemple #14
0
// 取消息数组
func (self *DynamicMessage) GetRepeatedMessage(fd *pbmeta.FieldDescriptor) []*DynamicMessage {

	if fd.Type() != pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		return nil
	}

	fv, ok := self.fieldMap[fd]

	if !ok {
		return nil
	}

	if !fd.IsRepeated() {
		return nil
	}

	return fv.msgArray
}
Exemple #15
0
// 取单一值
func (self *DynamicMessage) GetValue(fd *pbmeta.FieldDescriptor) (string, bool) {

	if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
		return "", false
	}

	fv, ok := self.fieldMap[fd]

	if !ok {
		return "", false
	}

	if fd.IsRepeated() {
		return "", false
	}

	return fv.value, true
}
Exemple #16
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)
	}
}
Exemple #17
0
func fieldTypeString(fd *pbmeta.FieldDescriptor) string {

	var ret string

	switch fd.Type() {

	case pbprotos.FieldDescriptorProto_TYPE_INT32:
		ret = "int"
	case pbprotos.FieldDescriptorProto_TYPE_UINT32:
		ret = "uint"
	case pbprotos.FieldDescriptorProto_TYPE_BOOL:
		ret = "bool"
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT:
		ret = "float"
	case pbprotos.FieldDescriptorProto_TYPE_DOUBLE:
		ret = "double"
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		ret = "string"
	case pbprotos.FieldDescriptorProto_TYPE_INT64:
		ret = "long"
	case pbprotos.FieldDescriptorProto_TYPE_ENUM,
		pbprotos.FieldDescriptorProto_TYPE_MESSAGE:
		ret = fd.FullTypeName()
	default:
		ret = "unknown"
	}

	if fd.IsRepeated() {
		return fmt.Sprintf("global::System.Collections.Generic.List<%s>", ret)
	}

	return ret
}
Exemple #18
0
// 单一消息
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
	}

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

		return false
	}

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

	return true
}
Exemple #19
0
func jsonvalueWrapper(fd *pbmeta.FieldDescriptor, value string) string {

	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		return util.StringEscape(value)
	case pbprotos.FieldDescriptorProto_TYPE_ENUM:
		ed := fd.EnumDesc()

		if ed == nil {
			return "0"
		}

		evd := ed.ValueByName(value)

		if evd == nil {
			return "0"
		}

		return strconv.Itoa(int(evd.Value()))

	}

	return value
}
Exemple #20
0
// 删除值
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
}
Exemple #21
0
func GetDefaultValue(fd *pbmeta.FieldDescriptor) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT,
		pbprotos.FieldDescriptorProto_TYPE_INT64,
		pbprotos.FieldDescriptorProto_TYPE_UINT64,
		pbprotos.FieldDescriptorProto_TYPE_INT32,
		pbprotos.FieldDescriptorProto_TYPE_UINT32:
		if fd.DefaultValue() != "" && fd.DefaultValue() != "0" {
			break
		}

		return "0"

	case pbprotos.FieldDescriptorProto_TYPE_BOOL:

		if fd.DefaultValue() != "" && fd.DefaultValue() != "false" {
			break
		}

		return "false"

	case pbprotos.FieldDescriptorProto_TYPE_STRING:

		if fd.DefaultValue() != "" {
			break
		}

		return ""

	case pbprotos.FieldDescriptorProto_TYPE_ENUM:

		ed := fd.EnumDesc()

		// 好奇葩
		if ed.ValueCount() == 0 {
			return ""
		}

		evd := ed.Value(0)

		if fd.DefaultValue() != "" && fd.DefaultValue() != evd.Name() {
			break
		}

		return evd.Name()

	}

	return fd.DefaultValue()
}
Exemple #22
0
func getDefaultValue(fd *pbmeta.FieldDescriptor) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_INT32:
		return wrapDefaultValue(fd, "Int32")
	case pbprotos.FieldDescriptorProto_TYPE_UINT32:
		return wrapDefaultValue(fd, "UInt32")
	case pbprotos.FieldDescriptorProto_TYPE_BOOL:
		return wrapDefaultValue(fd, "bool")
	case pbprotos.FieldDescriptorProto_TYPE_FLOAT:
		return wrapDefaultValue(fd, "float")
	case pbprotos.FieldDescriptorProto_TYPE_DOUBLE:
		return wrapDefaultValue(fd, "double")
	case pbprotos.FieldDescriptorProto_TYPE_INT64:
		return wrapDefaultValue(fd, "Int64")
	case pbprotos.FieldDescriptorProto_TYPE_UINT64:
		return wrapDefaultValue(fd, "UInt64")
	case pbprotos.FieldDescriptorProto_TYPE_BYTES:
		return wrapDefaultValue(fd, "byte[]")
	case pbprotos.FieldDescriptorProto_TYPE_FIXED64:
		return wrapDefaultValue(fd, "UInt64")
	case pbprotos.FieldDescriptorProto_TYPE_FIXED32:
		return wrapDefaultValue(fd, "UInt32")
	case pbprotos.FieldDescriptorProto_TYPE_SFIXED64:
		return wrapDefaultValue(fd, "Int64")
	case pbprotos.FieldDescriptorProto_TYPE_SFIXED32:
		return wrapDefaultValue(fd, "Int32")
	case pbprotos.FieldDescriptorProto_TYPE_SINT64:
		return wrapDefaultValue(fd, "Int64")
	case pbprotos.FieldDescriptorProto_TYPE_SINT32:
		return wrapDefaultValue(fd, "Int32")

	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		v := strings.TrimSpace(fd.DefaultValue())
		if v != "" {
			return "@" + strconv.Quote(fd.DefaultValue())
		}

		return strconv.Quote(fd.DefaultValue())
	case pbprotos.FieldDescriptorProto_TYPE_ENUM:
		ed := fd.EnumDesc()

		if ed == nil {
			return fd.DefaultValue()
		}

		if ed.ValueCount() > 0 {

			var defaultValue string

			// 有defaultvalue, 直接取值, 否则取枚举第一个
			if v := strings.TrimSpace(fd.DefaultValue()); v != "" {
				defaultValue = v
			} else {
				defaultValue = ed.Value(0).Name()
			}

			return fmt.Sprintf("%s.%s", fd.FullTypeName(), defaultValue)
		}
	case pbprotos.FieldDescriptorProto_TYPE_MESSAGE:
		return "null"
	}

	return "unknown"
}
Exemple #23
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("readonly ", typeStr, " ", memberVar, " = new ", typeStr, "();")

	} else {

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

		if use_hasField {
			gen.Println("bool _has", fd.Name(), " = false;")
		}

	}

	// [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() {

		if use_hasField {
			gen.Println("set { ", memberVar, " = value; ")
			gen.Println("      _has", fd.Name(), " = true;")
			gen.Println("}")
		} else {
			gen.Println("set { ", memberVar, " = value; }")
		}

	}

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

	gen.Println()

	if use_hasField && !fd.IsRepeated() {
		gen.Println("public bool Has", fd.Name())
		gen.Println("{")
		gen.In()
		gen.Println("get { return _has", fd.Name(), "; }")
		gen.Println("set { _has", fd.Name(), " = value; }")
		gen.Out()
		gen.Println("}")
	}

}
Exemple #24
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
}
Exemple #25
0
func (self *jsonWriter) RepeatedValueBegin(fd *pbmeta.FieldDescriptor) {
	self.printer.WriteString(fmt.Sprintf("\"%s\" : [", fd.Name()))
}
Exemple #26
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
}
Exemple #27
0
// 设置单一值
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() {

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

		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
}
Exemple #28
0
func makeValue(fd *pbmeta.FieldDescriptor, value string) string {
	return fmt.Sprintf("%s: %s", fd.Name(), valueWrapper(fd, value))
}
Exemple #29
0
// 普通值
func (self *pbtWriter) WriteValue(fd *pbmeta.FieldDescriptor, value string, indent int) {

	self.printer.WriteString(fmt.Sprintf("%s: %s", fd.Name(), valueWrapper(fd, value)))
}