From ab4b4d5ed589c8ca18f521cb44c35132159ea5e9 Mon Sep 17 00:00:00 2001 From: tbxark Date: Sun, 10 Nov 2024 12:45:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20entproto=E9=BB=98=E8=AE=A4json=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=8F=AA=E6=94=AF=E6=8C=81=E5=9F=BA=E7=A1=80=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=88=87=E7=89=87,=E5=85=B6=E4=BD=99=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=BC=BA=E7=9C=81=E7=94=A8=E5=AD=97=E8=8A=82=E5=AD=98?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/ent-gen-proto/main.go | 23 ++++++++++++++++++++++- contrib/ent-gen-proto/schema/test.go | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/contrib/ent-gen-proto/main.go b/contrib/ent-gen-proto/main.go index 149d698..50540e5 100644 --- a/contrib/ent-gen-proto/main.go +++ b/contrib/ent-gen-proto/main.go @@ -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 @@ -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": {}, +} diff --git a/contrib/ent-gen-proto/schema/test.go b/contrib/ent-gen-proto/schema/test.go index 7d0ce6c..b604253 100644 --- a/contrib/ent-gen-proto/schema/test.go +++ b/contrib/ent-gen-proto/schema/test.go @@ -15,6 +15,8 @@ type Extra struct { type Level int +type Role string + const ( Unknown Level = iota Low