github.com/yoogoc/kratos-scaffold@v0.0.0-20240402032722-a538b3c18955/pkg/field/field_type.go (about)

     1  package field
     2  
     3  type TypeField int
     4  
     5  const (
     6  	TypeDouble TypeField = iota
     7  	TypeFloat
     8  	TypeInt32
     9  	TypeInt64
    10  	TypeUint32
    11  	TypeUint64
    12  	TypeBool
    13  	TypeString
    14  	TypeText
    15  	TypeTime
    16  	TypeDate
    17  )
    18  
    19  const DefaultPrimaryFieldType = TypeInt64
    20  
    21  var (
    22  	types = map[string]TypeField{
    23  		"float64": TypeDouble,
    24  		"float32": TypeFloat,
    25  		"int32":   TypeInt32,
    26  		"int64":   TypeInt64,
    27  		"uint32":  TypeUint32,
    28  		"uint64":  TypeUint64,
    29  		"bool":    TypeBool,
    30  		"string":  TypeString,
    31  		"text":    TypeText,
    32  		"time":    TypeTime,
    33  		"date":    TypeDate,
    34  	}
    35  
    36  	typeNames = [...]string{
    37  		TypeDouble: "float64",
    38  		TypeFloat:  "float32",
    39  		TypeInt32:  "int32",
    40  		TypeInt64:  "int64",
    41  		TypeUint32: "uint32",
    42  		TypeUint64: "uint64",
    43  		TypeBool:   "bool",
    44  		TypeString: "string",
    45  		TypeText:   "string",
    46  		TypeTime:   "time.Time",
    47  		TypeDate:   "time.Time",
    48  	}
    49  
    50  	typeEntSchemaNames = [...]string{
    51  		TypeDouble: "Float",
    52  		TypeFloat:  "Float32",
    53  		TypeInt32:  "Int32",
    54  		TypeInt64:  "Int64",
    55  		TypeUint32: "Uint32",
    56  		TypeUint64: "Uint64",
    57  		TypeBool:   "Bool",
    58  		TypeString: "String",
    59  		TypeText:   "String",
    60  		TypeTime:   "Time",
    61  		TypeDate:   "Time",
    62  	}
    63  
    64  	typeMysqlNames = [...]string{
    65  		TypeDouble: "numeric",
    66  		TypeFloat:  "numeric",
    67  		TypeInt32:  "int",
    68  		TypeInt64:  "bigint",
    69  		TypeUint32: "int",
    70  		TypeUint64: "bigint",
    71  		TypeBool:   "tinyint",
    72  		TypeString: "varchar(255)",
    73  		TypeText:   "text",
    74  		TypeTime:   "timestamp",
    75  		TypeDate:   "timestamp",
    76  	}
    77  
    78  	typePramNames = [...]string{
    79  		TypeDouble: "*float64",
    80  		TypeFloat:  "*float32",
    81  		TypeInt32:  "*int32",
    82  		TypeInt64:  "*int64",
    83  		TypeUint32: "*uint32",
    84  		TypeUint64: "*uint64",
    85  		TypeBool:   "*bool",
    86  		TypeString: "*string",
    87  		TypeTime:   "*time.Time",
    88  		TypeDate:   "*time.Time",
    89  	}
    90  
    91  	MaybeGoPackages = []string{
    92  		"time",
    93  	}
    94  
    95  	typeProtoNames = [...]string{
    96  		TypeDouble: "double",
    97  		TypeFloat:  "float",
    98  		TypeInt32:  "int32",
    99  		TypeInt64:  "int64",
   100  		TypeUint32: "uint32",
   101  		TypeUint64: "uint64",
   102  		TypeBool:   "bool",
   103  		TypeString: "string",
   104  		TypeText:   "string",
   105  		TypeTime:   "string",
   106  		TypeDate:   "string",
   107  	}
   108  
   109  	typeParamProtoNames = [...]string{
   110  		TypeDouble: "optional double",
   111  		TypeFloat:  "optional float",
   112  		TypeInt32:  "optional int32",
   113  		TypeInt64:  "optional int64",
   114  		TypeUint32: "optional uint32",
   115  		TypeUint64: "optional uint64",
   116  		TypeBool:   "optional bool",
   117  		TypeString: "optional string",
   118  		TypeText:   "optional string",
   119  		TypeTime:   "optional string",
   120  		TypeDate:   "optional string",
   121  	}
   122  
   123  	typeProtoPackages = [...]string{
   124  		TypeDouble: "",
   125  		TypeFloat:  "",
   126  		TypeInt32:  "",
   127  		TypeInt64:  "",
   128  		TypeUint32: "",
   129  		TypeUint64: "",
   130  		TypeBool:   "",
   131  		TypeString: "",
   132  		TypeText:   "",
   133  		TypeTime:   "",
   134  		TypeDate:   "",
   135  	}
   136  
   137  	typeProtoParamPackages = [...]string{
   138  		TypeDouble: "",
   139  		TypeFloat:  "",
   140  		TypeInt32:  "",
   141  		TypeInt64:  "",
   142  		TypeUint32: "",
   143  		TypeUint64: "",
   144  		TypeBool:   "",
   145  		TypeString: "",
   146  		TypeText:   "",
   147  		TypeTime:   "",
   148  	}
   149  
   150  	typeBiz2Proto = [...]string{
   151  		TypeDouble: "wrapperspb.Double(%s)",
   152  		TypeFloat:  "wrapperspb.Float(%s)",
   153  		TypeInt32:  "wrapperspb.Int32(%s)",
   154  		TypeInt64:  "wrapperspb.Int64(%s)",
   155  		TypeUint32: "wrapperspb.UInt32(%s)",
   156  		TypeUint64: "wrapperspb.UInt64(%s)",
   157  		TypeBool:   "wrapperspb.Bool(%s)",
   158  		TypeString: "wrapperspb.String(%s)",
   159  		TypeTime:   "%s.Format(time.DateTime)",
   160  		TypeDate:   "%s.Format(time.DateOnly)",
   161  	}
   162  )
   163  
   164  func (t TypeField) String() string {
   165  	return typeNames[t]
   166  }
   167  
   168  func (t TypeField) IsTime() bool {
   169  	return t == TypeTime
   170  }
   171  
   172  func (t TypeField) StringParam() string {
   173  	return typePramNames[t]
   174  }
   175  
   176  func (t TypeField) StringEnt() string {
   177  	return typeEntSchemaNames[t]
   178  }
   179  
   180  func (t TypeField) StringMysql() string {
   181  	return typeMysqlNames[t]
   182  }
   183  
   184  func (t TypeField) StringProto() string {
   185  	return typeProtoNames[t]
   186  }
   187  
   188  func (t TypeField) StringProtoParam() string {
   189  	return typeParamProtoNames[t]
   190  }
   191  
   192  func (t TypeField) ImportProto() string {
   193  	return typeProtoPackages[t]
   194  }
   195  
   196  func (t TypeField) ImportProtoParam() string {
   197  	return typeProtoParamPackages[t]
   198  }
   199  
   200  func (t TypeField) Biz2Proto() string {
   201  	return typeBiz2Proto[t]
   202  }