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" }
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() }
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 }
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 }