diff --git a/cmd/gf/internal/cmd/cmd.go b/cmd/gf/internal/cmd/cmd.go index c494e9672..7d20aedda 100644 --- a/cmd/gf/internal/cmd/cmd.go +++ b/cmd/gf/internal/cmd/cmd.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_build.go b/cmd/gf/internal/cmd/cmd_build.go index 4340ff388..e43ecc208 100644 --- a/cmd/gf/internal/cmd/cmd_build.go +++ b/cmd/gf/internal/cmd/cmd_build.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_docker.go b/cmd/gf/internal/cmd/cmd_docker.go index 0d042006c..ddd78bcbb 100644 --- a/cmd/gf/internal/cmd/cmd_docker.go +++ b/cmd/gf/internal/cmd/cmd_docker.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_env.go b/cmd/gf/internal/cmd/cmd_env.go index 2a5f6e0d2..76251068e 100644 --- a/cmd/gf/internal/cmd/cmd_env.go +++ b/cmd/gf/internal/cmd/cmd_env.go @@ -1,14 +1,21 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( "bytes" "context" + "github.com/olekukonko/tablewriter" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gproc" "github.com/gogf/gf/v2/text/gregex" "github.com/gogf/gf/v2/text/gstr" - "github.com/olekukonko/tablewriter" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" ) diff --git a/cmd/gf/internal/cmd/cmd_fix.go b/cmd/gf/internal/cmd/cmd_fix.go index 8fda60f24..ce27b8ace 100644 --- a/cmd/gf/internal/cmd/cmd_fix.go +++ b/cmd/gf/internal/cmd/cmd_fix.go @@ -1,7 +1,14 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( "context" + "github.com/gogf/gf/v2/os/gproc" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" diff --git a/cmd/gf/internal/cmd/cmd_gen.go b/cmd/gf/internal/cmd/cmd_gen.go index 0ad663770..bf60e639e 100644 --- a/cmd/gf/internal/cmd/cmd_gen.go +++ b/cmd/gf/internal/cmd/cmd_gen.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( @@ -12,6 +18,7 @@ var ( type cGen struct { g.Meta `name:"gen" brief:"{cGenBrief}" dc:"{cGenDc}"` cGenDao + cGenEnums cGenPb cGenPbEntity cGenService diff --git a/cmd/gf/internal/cmd/cmd_gen_dao.go b/cmd/gf/internal/cmd/cmd_gen_dao.go index a64b23dc8..8862696e1 100644 --- a/cmd/gf/internal/cmd/cmd_gen_dao.go +++ b/cmd/gf/internal/cmd/cmd_gen_dao.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_gen_enums.go b/cmd/gf/internal/cmd/cmd_gen_enums.go new file mode 100644 index 000000000..841f8ef1d --- /dev/null +++ b/cmd/gf/internal/cmd/cmd_gen_enums.go @@ -0,0 +1,15 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package cmd + +import ( + "github.com/gogf/gf/cmd/gf/v2/internal/cmd/genenums" +) + +type ( + cGenEnums = genenums.CGenEnums +) diff --git a/cmd/gf/internal/cmd/cmd_gen_pb.go b/cmd/gf/internal/cmd/cmd_gen_pb.go index ff734f821..1557a60b9 100644 --- a/cmd/gf/internal/cmd/cmd_gen_pb.go +++ b/cmd/gf/internal/cmd/cmd_gen_pb.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import "github.com/gogf/gf/cmd/gf/v2/internal/cmd/genpb" diff --git a/cmd/gf/internal/cmd/cmd_gen_pbentity.go b/cmd/gf/internal/cmd/cmd_gen_pbentity.go index 545963512..a78d6f772 100644 --- a/cmd/gf/internal/cmd/cmd_gen_pbentity.go +++ b/cmd/gf/internal/cmd/cmd_gen_pbentity.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import "github.com/gogf/gf/cmd/gf/v2/internal/cmd/genpbentity" diff --git a/cmd/gf/internal/cmd/cmd_gen_service.go b/cmd/gf/internal/cmd/cmd_gen_service.go index 726770602..6b3c038d4 100644 --- a/cmd/gf/internal/cmd/cmd_gen_service.go +++ b/cmd/gf/internal/cmd/cmd_gen_service.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_init.go b/cmd/gf/internal/cmd/cmd_init.go index 8d0a0f224..164905715 100644 --- a/cmd/gf/internal/cmd/cmd_init.go +++ b/cmd/gf/internal/cmd/cmd_init.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_install.go b/cmd/gf/internal/cmd/cmd_install.go index 7f9040a80..382ce72ca 100644 --- a/cmd/gf/internal/cmd/cmd_install.go +++ b/cmd/gf/internal/cmd/cmd_install.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_pack.go b/cmd/gf/internal/cmd/cmd_pack.go index 2b63f02f9..6c9d555d1 100644 --- a/cmd/gf/internal/cmd/cmd_pack.go +++ b/cmd/gf/internal/cmd/cmd_pack.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_run.go b/cmd/gf/internal/cmd/cmd_run.go index 2bc2752cc..bcdbea290 100644 --- a/cmd/gf/internal/cmd/cmd_run.go +++ b/cmd/gf/internal/cmd/cmd_run.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_tpl.go b/cmd/gf/internal/cmd/cmd_tpl.go index cf568c82d..245031394 100644 --- a/cmd/gf/internal/cmd/cmd_tpl.go +++ b/cmd/gf/internal/cmd/cmd_tpl.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/cmd_up.go b/cmd/gf/internal/cmd/cmd_up.go index 47851a8d8..424ac0dfc 100644 --- a/cmd/gf/internal/cmd/cmd_up.go +++ b/cmd/gf/internal/cmd/cmd_up.go @@ -1,11 +1,18 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( "context" "fmt" + "runtime" + "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils" "github.com/gogf/gf/v2/container/gset" - "runtime" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" "github.com/gogf/gf/v2/frame/g" diff --git a/cmd/gf/internal/cmd/cmd_version.go b/cmd/gf/internal/cmd/cmd_version.go index feec2a6bd..8ab836f56 100644 --- a/cmd/gf/internal/cmd/cmd_version.go +++ b/cmd/gf/internal/cmd/cmd_version.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package cmd import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao.go b/cmd/gf/internal/cmd/gendao/gendao.go index e600d0772..6cdbb8e4d 100644 --- a/cmd/gf/internal/cmd/gendao/gendao.go +++ b/cmd/gf/internal/cmd/gendao/gendao.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao_clear.go b/cmd/gf/internal/cmd/gendao/gendao_clear.go index 4eb69d4c3..0a420dab9 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_clear.go +++ b/cmd/gf/internal/cmd/gendao/gendao_clear.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao_dao.go b/cmd/gf/internal/cmd/gendao/gendao_dao.go index 167ff6c46..a76126ac7 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_dao.go +++ b/cmd/gf/internal/cmd/gendao/gendao_dao.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao_do.go b/cmd/gf/internal/cmd/gendao/gendao_do.go index 482ad4398..706961227 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_do.go +++ b/cmd/gf/internal/cmd/gendao/gendao_do.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao_entity.go b/cmd/gf/internal/cmd/gendao/gendao_entity.go index 501a7a292..2907f9226 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_entity.go +++ b/cmd/gf/internal/cmd/gendao/gendao_entity.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index 65cdb81f4..4b52f1e8b 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gendao import ( @@ -5,11 +11,12 @@ import ( "context" "fmt" + "github.com/olekukonko/tablewriter" + "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gregex" "github.com/gogf/gf/v2/text/gstr" - "github.com/olekukonko/tablewriter" ) type generateStructDefinitionInput struct { diff --git a/cmd/gf/internal/cmd/genenums/genenums.go b/cmd/gf/internal/cmd/genenums/genenums.go new file mode 100644 index 000000000..0285a7db5 --- /dev/null +++ b/cmd/gf/internal/cmd/genenums/genenums.go @@ -0,0 +1,83 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package genenums + +import ( + "context" + "golang.org/x/tools/go/packages" + + "github.com/gogf/gf/cmd/gf/v2/internal/consts" + "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gfile" + "github.com/gogf/gf/v2/text/gstr" + "github.com/gogf/gf/v2/util/gtag" +) + +type ( + CGenEnums struct{} + CGenEnumsInput struct { + g.Meta `name:"enums" config:"{CGenEnumsConfig}" brief:"{CGenEnumsBrief}" eg:"{CGenEnumsEg}"` + Src string `name:"src" short:"s" dc:"source folder path to be parsed" d:"."` + Path string `name:"path" short:"p" dc:"output go file path storing enums content" d:"internal/boot/boot_enums.go"` + Prefix string `name:"prefix" short:"x" dc:"only exports packages that starts with specified prefix"` + } + CGenEnumsOutput struct{} +) + +const ( + CGenEnumsConfig = `gfcli.gen.enums` + CGenEnumsBrief = `parse go files in current project and generate enums go file` + CGenEnumsEg = ` +gf gen enums +gf gen enums -p internal/boot/boot_enums.go +gf gen enums -p internal/boot/boot_enums.go -s . +gf gen enums -x github.com/gogf +` +) + +func init() { + gtag.Sets(g.MapStrStr{ + `CGenEnumsEg`: CGenEnumsEg, + `CGenEnumsBrief`: CGenEnumsBrief, + `CGenEnumsConfig`: CGenEnumsConfig, + }) +} + +func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnumsOutput, err error) { + realPath := gfile.RealPath(in.Src) + if realPath == "" { + mlog.Fatalf(`source folder path "%s" does not exist`, in.Src) + } + err = gfile.Chdir(realPath) + if err != nil { + mlog.Fatal(err) + } + mlog.Printf(`scanning: %s`, realPath) + cfg := &packages.Config{ + Dir: realPath, + Mode: pkgLoadMode, + Tests: false, + } + pkgs, err := packages.Load(cfg) + if err != nil { + mlog.Fatal(err) + } + p := NewEnumsParser(in.Prefix) + p.ParsePackages(pkgs) + var enumsContent = gstr.ReplaceByMap(consts.TemplateGenEnums, g.MapStrStr{ + "{PackageName}": gfile.Basename(gfile.Dir(in.Path)), + "{EnumsJson}": "`" + p.Export() + "`", + }) + enumsContent = gstr.Trim(enumsContent) + if err = gfile.PutContents(in.Path, enumsContent); err != nil { + return + } + mlog.Printf(`generated: %s`, in.Path) + mlog.Print("done!") + return +} diff --git a/cmd/gf/internal/cmd/genenums/genenums_parser.go b/cmd/gf/internal/cmd/genenums/genenums_parser.go new file mode 100644 index 000000000..8a66acaf6 --- /dev/null +++ b/cmd/gf/internal/cmd/genenums/genenums_parser.go @@ -0,0 +1,140 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package genenums + +import ( + "go/constant" + "go/types" + "golang.org/x/tools/go/packages" + + "github.com/gogf/gf/v2/encoding/gjson" + "github.com/gogf/gf/v2/text/gstr" + "github.com/gogf/gf/v2/util/gconv" +) + +const pkgLoadMode = 0xffffff + +type EnumsParser struct { + enums []EnumItem + parsedPkg map[string]struct{} + prefix string +} + +type EnumItem struct { + Name string + Value string + Kind constant.Kind // String/Int/Bool/Float/Complex/Unknown + Type string // Pkg.ID + TypeName +} + +var standardPackages = make(map[string]struct{}) + +func init() { + stdPackages, err := packages.Load(nil, "std") + if err != nil { + panic(err) + } + for _, p := range stdPackages { + standardPackages[p.ID] = struct{}{} + } +} + +func NewEnumsParser(prefix string) *EnumsParser { + return &EnumsParser{ + enums: make([]EnumItem, 0), + parsedPkg: make(map[string]struct{}), + prefix: prefix, + } +} + +func (p *EnumsParser) ParsePackages(pkgs []*packages.Package) { + for _, pkg := range pkgs { + p.ParsePackage(pkg) + } +} + +func (p *EnumsParser) ParsePackage(pkg *packages.Package) { + // Ignore std packages. + if _, ok := standardPackages[pkg.ID]; ok { + return + } + // Ignore pared packages. + if _, ok := p.parsedPkg[pkg.ID]; ok { + return + } + p.parsedPkg[pkg.ID] = struct{}{} + // Only parse specified prefix. + if p.prefix != "" { + if !gstr.HasPrefix(pkg.ID, p.prefix) { + return + } + } + + var ( + scope = pkg.Types.Scope() + names = scope.Names() + ) + for _, name := range names { + con, ok := scope.Lookup(name).(*types.Const) + if !ok { + // Only constants can be enums. + continue + } + if !con.Exported() { + // Ignore unexported values. + continue + } + + var enumType = con.Type().String() + if !gstr.Contains(enumType, "/") { + // Ignore std types. + continue + } + var ( + enumName = con.Name() + enumValue = con.Val().ExactString() + enumKind = con.Val().Kind() + ) + if con.Val().Kind() == constant.String { + enumValue = constant.StringVal(con.Val()) + } + p.enums = append(p.enums, EnumItem{ + Name: enumName, + Value: enumValue, + Type: enumType, + Kind: enumKind, + }) + + } + for _, im := range pkg.Imports { + p.ParsePackage(im) + } +} + +func (p *EnumsParser) Export() string { + var typeEnumMap = make(map[string][]interface{}) + for _, enum := range p.enums { + if typeEnumMap[enum.Type] == nil { + typeEnumMap[enum.Type] = make([]interface{}, 0) + } + var value interface{} + switch enum.Kind { + case constant.Int: + value = gconv.Int64(enum.Value) + case constant.String: + value = enum.Value + case constant.Float: + value = gconv.Float64(enum.Value) + case constant.Bool: + value = gconv.Bool(enum.Value) + default: + value = enum.Value + } + typeEnumMap[enum.Type] = append(typeEnumMap[enum.Type], value) + } + return gjson.MustEncodeString(typeEnumMap) +} diff --git a/cmd/gf/internal/cmd/genpb/genpb.go b/cmd/gf/internal/cmd/genpb/genpb.go index 0806d9bb0..e1c4f8836 100644 --- a/cmd/gf/internal/cmd/genpb/genpb.go +++ b/cmd/gf/internal/cmd/genpb/genpb.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genpb import ( diff --git a/cmd/gf/internal/cmd/genpb/genpb_controller.go b/cmd/gf/internal/cmd/genpb/genpb_controller.go index 2dad3fc6e..cbeaf0203 100644 --- a/cmd/gf/internal/cmd/genpb/genpb_controller.go +++ b/cmd/gf/internal/cmd/genpb/genpb_controller.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genpb import ( diff --git a/cmd/gf/internal/cmd/genpb/genpb_tag.go b/cmd/gf/internal/cmd/genpb/genpb_tag.go index 5ebf69157..5bc401a6e 100644 --- a/cmd/gf/internal/cmd/genpb/genpb_tag.go +++ b/cmd/gf/internal/cmd/genpb/genpb_tag.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genpb import ( diff --git a/cmd/gf/internal/cmd/genpbentity/genpbentity.go b/cmd/gf/internal/cmd/genpbentity/genpbentity.go index b1eb68804..6649f3a4c 100644 --- a/cmd/gf/internal/cmd/genpbentity/genpbentity.go +++ b/cmd/gf/internal/cmd/genpbentity/genpbentity.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genpbentity import ( diff --git a/cmd/gf/internal/cmd/genservice/genservice.go b/cmd/gf/internal/cmd/genservice/genservice.go index 1d71bcae7..26ac93332 100644 --- a/cmd/gf/internal/cmd/genservice/genservice.go +++ b/cmd/gf/internal/cmd/genservice/genservice.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genservice import ( diff --git a/cmd/gf/internal/cmd/genservice/genservice_calculate.go b/cmd/gf/internal/cmd/genservice/genservice_calculate.go index b3e8f4e03..dea843a82 100644 --- a/cmd/gf/internal/cmd/genservice/genservice_calculate.go +++ b/cmd/gf/internal/cmd/genservice/genservice_calculate.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genservice import ( diff --git a/cmd/gf/internal/cmd/genservice/genservice_generate.go b/cmd/gf/internal/cmd/genservice/genservice_generate.go index 2ed0f5d0b..a592f6e7f 100644 --- a/cmd/gf/internal/cmd/genservice/genservice_generate.go +++ b/cmd/gf/internal/cmd/genservice/genservice_generate.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genservice import ( diff --git a/cmd/gf/internal/consts/consts.go b/cmd/gf/internal/consts/consts.go index ab72444a7..cece25d70 100644 --- a/cmd/gf/internal/consts/consts.go +++ b/cmd/gf/internal/consts/consts.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const ( diff --git a/cmd/gf/internal/consts/consts_gen_dao_template_dao.go b/cmd/gf/internal/consts/consts_gen_dao_template_dao.go index e2d3ec34a..a9b7ee2e5 100644 --- a/cmd/gf/internal/consts/consts_gen_dao_template_dao.go +++ b/cmd/gf/internal/consts/consts_gen_dao_template_dao.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplateGenDaoIndexContent = ` diff --git a/cmd/gf/internal/consts/consts_gen_dao_template_do.go b/cmd/gf/internal/consts/consts_gen_dao_template_do.go index 4847adc21..4880b4056 100644 --- a/cmd/gf/internal/consts/consts_gen_dao_template_do.go +++ b/cmd/gf/internal/consts/consts_gen_dao_template_do.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplateGenDaoDoContent = ` diff --git a/cmd/gf/internal/consts/consts_gen_dao_template_entity.go b/cmd/gf/internal/consts/consts_gen_dao_template_entity.go index 49a3311c5..afe15ea17 100644 --- a/cmd/gf/internal/consts/consts_gen_dao_template_entity.go +++ b/cmd/gf/internal/consts/consts_gen_dao_template_entity.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplateGenDaoEntityContent = ` diff --git a/cmd/gf/internal/consts/consts_gen_enums_template.go b/cmd/gf/internal/consts/consts_gen_enums_template.go new file mode 100644 index 000000000..e97f40de4 --- /dev/null +++ b/cmd/gf/internal/consts/consts_gen_enums_template.go @@ -0,0 +1,23 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package consts + +const TemplateGenEnums = ` +// ================================================================================ +// Code generated by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================ + +package {PackageName} + +import ( + "github.com/gogf/gf/v2/util/gtag" +) + +func init() { + gtag.SetGlobalEnums({EnumsJson}) +} +` diff --git a/cmd/gf/internal/consts/consts_gen_pbentity_template.go b/cmd/gf/internal/consts/consts_gen_pbentity_template.go index 922b48719..ad9218ab1 100644 --- a/cmd/gf/internal/consts/consts_gen_pbentity_template.go +++ b/cmd/gf/internal/consts/consts_gen_pbentity_template.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplatePbEntityMessageContent = ` diff --git a/cmd/gf/internal/consts/consts_gen_service_template.go b/cmd/gf/internal/consts/consts_gen_service_template.go index 1ab216a76..b7d6e52a0 100644 --- a/cmd/gf/internal/consts/consts_gen_service_template.go +++ b/cmd/gf/internal/consts/consts_gen_service_template.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplateGenServiceContentHead = ` diff --git a/cmd/gf/internal/consts/consts_gen_service_template_logic.go b/cmd/gf/internal/consts/consts_gen_service_template_logic.go index 147983438..8a32d6992 100644 --- a/cmd/gf/internal/consts/consts_gen_service_template_logic.go +++ b/cmd/gf/internal/consts/consts_gen_service_template_logic.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package consts const TemplateGenServiceLogicContent = ` diff --git a/cmd/gf/internal/packed/packed.go b/cmd/gf/internal/packed/packed.go index e20ab1e95..2c43d2765 100644 --- a/cmd/gf/internal/packed/packed.go +++ b/cmd/gf/internal/packed/packed.go @@ -1 +1,7 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package packed diff --git a/cmd/gf/internal/service/install.go b/cmd/gf/internal/service/install.go index aaa4a6d9d..914169ca6 100644 --- a/cmd/gf/internal/service/install.go +++ b/cmd/gf/internal/service/install.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package service import ( diff --git a/cmd/gf/internal/utility/allyes/allyes.go b/cmd/gf/internal/utility/allyes/allyes.go index 146eda4b4..f8f7d1ea4 100644 --- a/cmd/gf/internal/utility/allyes/allyes.go +++ b/cmd/gf/internal/utility/allyes/allyes.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package allyes import ( diff --git a/cmd/gf/internal/utility/mlog/mlog.go b/cmd/gf/internal/utility/mlog/mlog.go index 4bf00c50d..d7af6553f 100644 --- a/cmd/gf/internal/utility/mlog/mlog.go +++ b/cmd/gf/internal/utility/mlog/mlog.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package mlog import ( diff --git a/cmd/gf/internal/utility/utils/utils.go b/cmd/gf/internal/utility/utils/utils.go index 7ef00d6b0..e8ea446a8 100644 --- a/cmd/gf/internal/utility/utils/utils.go +++ b/cmd/gf/internal/utility/utils/utils.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package utils import ( diff --git a/cmd/gf/internal/utility/utils/utils_http_download.go b/cmd/gf/internal/utility/utils/utils_http_download.go index e8f5927af..2117b4afa 100644 --- a/cmd/gf/internal/utility/utils/utils_http_download.go +++ b/cmd/gf/internal/utility/utils/utils_http_download.go @@ -1,14 +1,21 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package utils import ( "fmt" - "github.com/gogf/gf/v2/errors/gerror" "io" "net/http" "os" "strconv" "time" + "github.com/gogf/gf/v2/errors/gerror" + "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" ) diff --git a/cmd/gf/main.go b/cmd/gf/main.go index 64b5fe116..7a29aa0e5 100644 --- a/cmd/gf/main.go +++ b/cmd/gf/main.go @@ -1,3 +1,9 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package main import ( diff --git a/net/goai/goai_shema_ref.go b/net/goai/goai_shema_ref.go index 316cf6968..a2766f118 100644 --- a/net/goai/goai_shema_ref.go +++ b/net/goai/goai_shema_ref.go @@ -7,11 +7,13 @@ package goai import ( + "fmt" "reflect" "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" + "github.com/gogf/gf/v2/util/gtag" ) type SchemaRefs []SchemaRef @@ -41,6 +43,8 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap err error oaiType = oai.golangTypeToOAIType(golangType) oaiFormat = oai.golangTypeToOAIFormat(golangType) + typeName = golangType.Name() + pkgPath = golangType.PkgPath() schemaRef = &SchemaRef{} schema = &Schema{ Type: oaiType, @@ -48,6 +52,23 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap XExtensions: make(XExtensions), } ) + if pkgPath == "" { + switch golangType.Kind() { + case reflect.Ptr, reflect.Array, reflect.Slice: + pkgPath = golangType.Elem().PkgPath() + typeName = golangType.Elem().Name() + } + } + + // Type enums. + var typeId = fmt.Sprintf(`%s.%s`, pkgPath, typeName) + if enums := gtag.GetEnumsByType(typeId); enums != "" { + schema.Enum = make([]interface{}, 0) + if err = json.Unmarshal([]byte(enums), &schema.Enum); err != nil { + return nil, err + } + } + if len(tagMap) > 0 { if err := oai.tagMapToSchema(tagMap, schema); err != nil { return nil, err diff --git a/net/goai/goai_z_unit_test.go b/net/goai/goai_z_unit_test.go index 4f52c7c25..69fbbfc80 100644 --- a/net/goai/goai_z_unit_test.go +++ b/net/goai/goai_z_unit_test.go @@ -12,11 +12,13 @@ import ( "net/http" "testing" + "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/net/goai" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/util/gmeta" + "github.com/gogf/gf/v2/util/gtag" ) func Test_Basic(t *testing.T) { @@ -1123,3 +1125,42 @@ func Test_EmptyJsonNameWithOmitEmpty(t *testing.T) { t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("None"), nil) }) } + +func Test_Enums(t *testing.T) { + type Status string + const ( + StatusA Status = "a" + StatusB Status = "b" + ) + type Req struct { + gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default"` + Name string `dc:"实例名称" json:",omitempty"` + Status1 Status `dc:"状态1" json:",omitempty"` + Status2 *Status `dc:"状态2" json:",omitempty"` + Status3 []Status `dc:"状态2" json:",omitempty"` + Status4 []*Status `dc:"状态2" json:",omitempty"` + } + + gtest.C(t, func(t *gtest.T) { + var ( + err error + oai = goai.New() + req = new(Req) + ) + err = gtag.SetGlobalEnums(gjson.MustEncodeString(g.Map{ + "github.com/gogf/gf/v2/net/goai_test.Status": []interface{}{StatusA, StatusB}, + })) + t.AssertNil(err) + + err = oai.Add(goai.AddInput{ + Object: req, + }) + t.AssertNil(err) + var reqKey = "github.com.gogf.gf.v2.net.goai_test.Req" + t.AssertNE(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Name"), nil) + t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status1").Value.Enum, g.Slice{"a", "b"}) + t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status2").Value.Enum, g.Slice{"a", "b"}) + t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status3").Value.Items.Value.Enum, g.Slice{"a", "b"}) + t.Assert(oai.Components.Schemas.Get(reqKey).Value.Properties.Get("Status4").Value.Items.Value.Enum, g.Slice{"a", "b"}) + }) +} diff --git a/util/gtag/gtag_enums.go b/util/gtag/gtag_enums.go new file mode 100644 index 000000000..3ab37462d --- /dev/null +++ b/util/gtag/gtag_enums.go @@ -0,0 +1,28 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gtag + +import ( + "github.com/gogf/gf/v2/internal/json" +) + +var ( + // Type name => enums json. + enumsMap = make(map[string]json.RawMessage) +) + +// SetGlobalEnums sets the global enums into package. +// Note that this operation is not concurrent safety. +func SetGlobalEnums(enumsJson string) error { + return json.Unmarshal([]byte(enumsJson), &enumsMap) +} + +// GetEnumsByType retrieves and returns the stored enums json by type name. +// The type name is like: github.com/gogf/gf/v2/encoding/gjson.ContentType +func GetEnumsByType(typeName string) string { + return string(enumsMap[typeName]) +}