github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/strfmt/internal/main/main.go (about) 1 package main 2 3 import ( 4 "go/ast" 5 "go/parser" 6 "go/token" 7 "reflect" 8 "sort" 9 "strings" 10 11 g "github.com/machinefi/w3bstream/pkg/depends/gen/codegen" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/validator" 13 "github.com/machinefi/w3bstream/pkg/depends/x/pkgx" 14 "github.com/machinefi/w3bstream/pkg/depends/x/stringsx" 15 ) 16 17 func main() { 18 fset := token.NewFileSet() 19 f, _ := parser.ParseFile(fset, "strfmt.go", nil, parser.ParseComments) 20 file := g.NewFile("strfmt", "strfmt_generated.go") 21 22 regexps := make([]string, 0) 23 for key, obj := range f.Scope.Objects { 24 if obj.Kind == ast.Con { 25 regexps = append(regexps, key) 26 } 27 } 28 sort.Strings(regexps) 29 30 snippets := make([]g.Snippet, 0) 31 for _, key := range regexps { 32 var ( 33 name = strings.Replace(key, "regexpString", "", 1) 34 validatorName = strings.Replace(stringsx.LowerSnakeCase(name), "_", "-", -1) 35 validatorAlias = stringsx.LowerCamelCase(name) 36 args = []g.Snippet{g.Ident(key), g.Valuer(validatorName)} 37 prefix = stringsx.UpperCamelCase(name) 38 snippet g.Snippet 39 ) 40 if validatorName != validatorAlias { 41 args = append(args, g.Valuer(validatorAlias)) 42 } 43 snippet = g.Func().Named("init").Do( 44 g.Ref( 45 g.Ident(file.Use(pkg, "DefaultFactory")), 46 g.Call( 47 "Register", 48 g.Ident(prefix+"Validator"), 49 ), 50 ), 51 ) 52 snippets = append(snippets, snippet) 53 snippet = g.DeclVar( 54 g.Assign(g.Var(nil, prefix+"Validator")). 55 By(g.Call(file.Use(pkg, "NewRegexpStrfmtValidator"), args...)), 56 ) 57 snippets = append(snippets, snippet) 58 59 } 60 file.WriteSnippet(snippets...) 61 _, _ = file.Write() 62 } 63 64 var pkg = pkgx.Import(reflect.TypeOf(validator.Rule{}).PkgPath())