Example #1
0
File: msg.go Project: davyxu/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() {

		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
}
Example #2
0
File: msg.go Project: jwk000/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() {
		return false
	}

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

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

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

	return value
}
Example #5
0
File: pbt.go Project: jwk000/tabtoy
func valueWrapper(fd *pbmeta.FieldDescriptor, value string) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_STRING:
		return fmt.Sprintf("\"%s\"", value)
	}

	return value
}
Example #6
0
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"
}
Example #7
0
File: msg.go Project: davyxu/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() {

		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
}
Example #8
0
func getDefaultValue(fd *pbmeta.FieldDescriptor) string {
	switch fd.Type() {
	case pbprotos.FieldDescriptorProto_TYPE_INT32:
		return wrapDefaultValue(fd, "int")
	case pbprotos.FieldDescriptorProto_TYPE_UINT32:
		return wrapDefaultValue(fd, "uint")
	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, "long")
	case pbprotos.FieldDescriptorProto_TYPE_UINT64:
		return wrapDefaultValue(fd, "ulong")
	case pbprotos.FieldDescriptorProto_TYPE_BYTES:
		return wrapDefaultValue(fd, "byte[]")
	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"
}
Example #9
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()
}
Example #10
0
File: lua.go Project: 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))
}
Example #11
0
File: msg.go Project: 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
}
Example #12
0
File: msg.go Project: davyxu/tabtoy
// 取消息数组
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
}
Example #13
0
File: msg.go Project: davyxu/tabtoy
// 取单一值
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
}
Example #14
0
File: msg.go Project: 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
}
Example #15
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
}
Example #16
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
}