Skip to content

Commit

Permalink
chore: 使用go:linkname调用私有函数替代go mod replace
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 9, 2024
1 parent deacbef commit b32a9fa
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 101 deletions.
38 changes: 0 additions & 38 deletions api/entpb/entpb.pb_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

[ent-gen-proto](./ent-gen-proto/README.md): This is a tool to generate proto files from ent schema.

[protoc-gen-sphere](./protoc-gen-sphere/README.md): This is a tool to generate `gin` code from proto files
[protoc-gen-sphere](./protoc-gen-sphere/README.md): This is a tool to generate http server code from proto files

[protoc-gen-route](protoc-gen-route/README.md): This is a tool to generate common route code from proto files
[protoc-gen-route](./protoc-gen-route/README.md): This is a tool to generate common route code from proto files
9 changes: 3 additions & 6 deletions contrib/ent-gen-proto/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ module github.com/tbxark/sphere/contrib/ent-gen-proto

go 1.23.2

replace entgo.io/contrib => github.com/TBXark/ent-contrib v0.6.1-0.20241025161307-11e08b46d83e

require (
entgo.io/contrib v0.0.0-00010101000000-000000000000
entgo.io/contrib v0.6.0
entgo.io/ent v0.14.1
)

require (
ariga.io/atlas v0.28.1 // indirect
ariga.io/atlas v0.25.1-0.20240717145915-af51d3945208 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -30,5 +27,5 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
14 changes: 6 additions & 8 deletions contrib/ent-gen-proto/go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
ariga.io/atlas v0.28.1 h1:cNE0FYmoYs1u4KF+FGnp2on1srhM6FDpjaCgL7Rd8/c=
ariga.io/atlas v0.28.1/go.mod h1:LOOp18LCL9r+VifvVlJqgYJwYl271rrXD9/wIyzJ8sw=
ariga.io/atlas v0.25.1-0.20240717145915-af51d3945208 h1:ixs1c/fAXGS3mTdalyKQrtvfkFjgChih/unX66YTzYk=
ariga.io/atlas v0.25.1-0.20240717145915-af51d3945208/go.mod h1:KPLc7Zj+nzoXfWshrcY1RwlOh94dsATQEy4UPrF2RkM=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
entgo.io/contrib v0.6.0 h1:xfo4TbJE7sJZWx7BV7YrpSz7IPFvS8MzL3fnfzZjKvQ=
entgo.io/contrib v0.6.0/go.mod h1:3qWIseJ/9Wx2Hu5zVh15FDzv7d/UvKNcYKdViywWCQg=
entgo.io/ent v0.14.1 h1:fUERL506Pqr92EPHJqr8EYxbPioflJo6PudkrEA8a/s=
entgo.io/ent v0.14.1/go.mod h1:MH6XLG0KXpkcDQhKiHfANZSzR55TJyPL5IGNpI8wpco=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/TBXark/ent-contrib v0.6.1-0.20241025161307-11e08b46d83e h1:sxHAcIm0v/5DMgpeuBaMi3ircytOGAtWB7tBVgO9AWM=
github.com/TBXark/ent-contrib v0.6.1-0.20241025161307-11e08b46d83e/go.mod h1:3qWIseJ/9Wx2Hu5zVh15FDzv7d/UvKNcYKdViywWCQg=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -160,8 +158,8 @@ google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
75 changes: 34 additions & 41 deletions contrib/ent-gen-proto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
"path/filepath"
"reflect"
"sort"
_ "unsafe"
)

//go:linkname generate entgo.io/contrib/entproto.(*Extension).generate
func generate(extension *entproto.Extension, g *gen.Graph) error

func main() {
var (
schemaPath = flag.String("path", "./internal/pkg/database/ent/schema", "path to schema directory")
protoDir = flag.String("proto", "./proto", "path to proto directory")
ignoreOptional = flag.Bool("ignore-optional", true, "ignore optional keyword")
ignoreOptional = flag.Bool("ignore-optional", true, "ignore optional keyword, use zero value instead")
autoAddAnnotation = flag.Bool("auto-annotation", true, "auto add annotation to the schema")
enumUseRawType = flag.Bool("enum-raw-type", true, "use string for enum")
help = flag.Bool("help", false, "show help")
Expand Down Expand Up @@ -71,7 +75,7 @@ func RunProtoGen(schemaPath string, protoDir string, ignoreOptional, autoAddAnno
if fd.IsEnum() {
if enumUseRawType {
if fd.HasGoType() {
fd.Type.Type = reflectKind2FieldType(fd.Type.RType.Kind)
fd.Type.Type = reflectKind2FieldType[fd.Type.RType.Kind]
} else {
fd.Type.Type = field.TypeString
}
Expand All @@ -92,54 +96,43 @@ func RunProtoGen(schemaPath string, protoDir string, ignoreOptional, autoAddAnno
}
}
extension, err := entproto.NewExtension(
entproto.EnableOptional(),
entproto.WithProtoDir(protoDir),
entproto.SkipGenFile(),
)
if err != nil {
log.Fatalf("entproto: failed creating entproto extension: %v", err)
}
err = extension.Generate(graph)
err = generate(extension, graph)
if err != nil {
log.Fatalf("entproto: failed generating protos: %s", err)
}
}

func reflectKind2FieldType(kind reflect.Kind) field.Type {
switch kind {
case reflect.Bool:
return field.TypeBool
case reflect.Int:
return field.TypeInt
case reflect.Int8:
return field.TypeInt8
case reflect.Int16:
return field.TypeInt16
case reflect.Int32:
return field.TypeInt32
case reflect.Int64:
return field.TypeInt64
case reflect.Uint:
return field.TypeUint
case reflect.Uint8:
return field.TypeUint8
case reflect.Uint16:
return field.TypeUint16
case reflect.Uint32:
return field.TypeUint32
case reflect.Uint64:
return field.TypeUint64
case reflect.Float32:
return field.TypeFloat32
case reflect.Float64:
return field.TypeFloat64
case reflect.String:
return field.TypeString
case reflect.Slice:
return field.TypeBytes
case reflect.Struct:
return field.TypeJSON
default:
return field.TypeOther
}
var reflectKind2FieldType = map[reflect.Kind]field.Type{
reflect.Bool: field.TypeBool,
reflect.Int: field.TypeInt,
reflect.Int8: field.TypeInt8,
reflect.Int16: field.TypeInt16,
reflect.Int32: field.TypeInt32,
reflect.Int64: field.TypeInt64,
reflect.Uint: field.TypeUint,
reflect.Uint8: field.TypeUint8,
reflect.Uint16: field.TypeUint16,
reflect.Uint32: field.TypeUint32,
reflect.Uint64: field.TypeUint64,
reflect.Uintptr: field.TypeUint,
reflect.Float32: field.TypeFloat32,
reflect.Float64: field.TypeFloat64,
reflect.Complex64: field.TypeOther,
reflect.Complex128: field.TypeOther,
reflect.Array: field.TypeJSON,
reflect.Chan: field.TypeOther,
reflect.Func: field.TypeOther,
reflect.Interface: field.TypeJSON,
reflect.Map: field.TypeJSON,
reflect.Pointer: field.TypeJSON,
reflect.Slice: field.TypeJSON,
reflect.String: field.TypeString,
reflect.Struct: field.TypeJSON,
reflect.UnsafePointer: field.TypeOther,
}
27 changes: 27 additions & 0 deletions contrib/ent-gen-proto/schema/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
)

type Extra struct {
Key string `json:"key"`
Vals string `json:"vals"`
}

type User struct {
ent.Schema
}

func (User) Fields() []ent.Field {
return []ent.Field{
field.Int64("id").Unique().Immutable().Comment("用户ID"),
field.String("username").Comment("用户名").MinLen(1),
field.String("remark").Optional().Default("").Comment("备注").MaxLen(30),
field.String("avatar").Comment("头像").Default(""),
field.String("phone").Optional().Default("").Comment("手机号").MaxLen(20),
field.Uint64("flags").Default(0).Comment("标记位"),
field.JSON("extra", Extra{}).Optional().Comment("额外信息"),
}
}
4 changes: 2 additions & 2 deletions contrib/protoc-gen-route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func generateFile(gen *protogen.Plugin, file *protogen.File, conf *Config) *prot
}
filename := file.GeneratedFilenamePrefix + conf.genFileSuffix
g := gen.NewGeneratedFile(filename, file.GoImportPath)
genBaseFileHeader(g, file)
genBaseFileHeader(file, g)
replaceTemplateIfNeed(conf)
generateFileContent(gen, file, g, conf)
return g
}

func genBaseFileHeader(g *protogen.GeneratedFile, file *protogen.File) {
func genBaseFileHeader(file *protogen.File, g *protogen.GeneratedFile) {
g.P("// Code generated by protoc-gen-route. DO NOT EDIT.")
g.P("// versions:")
if file.Proto.GetOptions().GetDeprecated() {
Expand Down
2 changes: 1 addition & 1 deletion contrib/protoc-gen-sphere/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# protoc-gen-sphere

This is a tool to generate `gin` code from proto files. This code is based on the [protoc-gen-go-http](https://github.com/go-kratos/kratos/tree/main/cmd/protoc-gen-go-http) tool.
This is a tool to generate http server code from proto files. This code is based on the [protoc-gen-go-http](https://github.com/go-kratos/kratos/tree/main/cmd/protoc-gen-go-http) tool.

## Installation

Expand Down
4 changes: 2 additions & 2 deletions contrib/protoc-gen-sphere/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
}
g.P("var _ = new(", ctxPackage.Ident("Context"), ")")
genConf := newGenConf(g, conf)
genGoImport(g, file, conf, genConf)
genGoImport(file, g, conf, genConf)
g.P()
for _, service := range file.Services {
genService(gen, file, g, service, genConf)
}
}

func genGoImport(g *protogen.GeneratedFile, file *protogen.File, conf *Config, genConf *genConfig) {
func genGoImport(file *protogen.File, g *protogen.GeneratedFile, conf *Config, genConf *genConfig) {
idents := []*GoIdent{
conf.routerType,
conf.contextType,
Expand Down
2 changes: 1 addition & 1 deletion doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## About

`Sphere` is a suggested server scaffolding that uses `ent` as the database structure definition and `proto` as the interface definition. It also provides a series of code and document generation tools, including `proto` files, `Swagger` documents, `TypeScript` clients, etc.
`Sphere` is a server scaffolding that uses `ent` as the database structure definition and `proto` as the interface definition. It also provides a series of code and document generation tools, including `proto` files, `Swagger` documents, `TypeScript` clients, etc.

You can fork this project and modify the code in proto, internal and cmd to implement your own business logic. Please do not modify the code in pkg. If necessary, please raise an issue or PR.

Expand Down

0 comments on commit b32a9fa

Please sign in to comment.