Skip to content

Commit

Permalink
fix: entproto默认json类型只支持基础类型切片,其余类型缺省用字节存储
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 10, 2024
1 parent 5a3aa3b commit ab4b4d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 22 additions & 1 deletion contrib/ent-gen-proto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func fixFieldType(fd *gen.Field, timeType string) field.Type {
case field.TypeJSON:
if fd.HasGoType() {
switch fd.Type.RType.Kind {
case reflect.Slice, reflect.Array, reflect.Map:
case reflect.Slice:
// 只支持基础类型的切片
if _, ok := buildInTypeSlice[fd.Type.RType.Ident]; ok {
return field.TypeJSON
}
return field.TypeJSON
default:
return field.TypeBytes
Expand Down Expand Up @@ -175,3 +179,20 @@ var reflectKind2FieldType = map[reflect.Kind]field.Type{
reflect.Struct: field.TypeJSON,
reflect.UnsafePointer: field.TypeOther,
}

var buildInTypeSlice = map[string]struct{}{
"[]int": {},
"[]int8": {},
"[]int16": {},
"[]int32": {},
"[]int64": {},
"[]uint": {},
"[]uint8": {},
"[]uint16": {},
"[]uint32": {},
"[]uint64": {},
"[]float32": {},
"[]float64": {},
"[]string": {},
"[]bool": {},
}
2 changes: 2 additions & 0 deletions contrib/ent-gen-proto/schema/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Extra struct {

type Level int

type Role string

const (
Unknown Level = iota
Low
Expand Down

0 comments on commit ab4b4d5

Please sign in to comment.