Example #1
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 #2
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"
}