|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "go/token" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "text/template" |
| 10 | + |
| 11 | + "github.com/sirkon/dst" |
| 12 | + "github.com/sirkon/dst/decorator" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + serverParamFileName = "server_params.go" |
| 17 | + commentEnumFieldsToReset = `// Enum: ["enabled","disabled"]` |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + var allEnumFields []string |
| 22 | + var packageName string |
| 23 | + args := Args{} |
| 24 | + err := args.Parse() |
| 25 | + if err != nil { |
| 26 | + log.Panic(err) |
| 27 | + } |
| 28 | + for _, fileName := range args.Files { |
| 29 | + packageName, allEnumFields = enumFieldsEnabledDisabled(fileName) |
| 30 | + if err != nil { |
| 31 | + log.Panic(err) |
| 32 | + } |
| 33 | + |
| 34 | + // check that all enum |
| 35 | + // Enum: [enabled disabled]" |
| 36 | + // fields have an entry in the ServerParamsPrepareForRuntimeMap |
| 37 | + missingFields, errMissingField := checkMissingEnumFields(allEnumFields) |
| 38 | + if errMissingField != nil { |
| 39 | + // Exit with error if any new enum field is found and its behavior is not defined in ServerParamsPrepareForRuntimeMap |
| 40 | + log.Printf("There are some fields in models/server_params.go that are Enum[enabled disabled] but missing in `ServerParamsPrepareForRuntimeMap`") |
| 41 | + log.Printf(" File location `cmd/server_params_runtime/server_parans_runtime_fields_behavior.go`") |
| 42 | + log.Printf("ACTION: Please add them to `ServerParamsPrepareForRuntimeMap`") |
| 43 | + log.Printf("Missing fields %v", missingFields) |
| 44 | + log.Printf("\t For doc, read cmd/server_params_runtime/README.md") |
| 45 | + |
| 46 | + os.Exit(1) |
| 47 | + } |
| 48 | + |
| 49 | + // Generate the reset function using the template |
| 50 | + tmpl, errTmpl := template.New("generate.tmpl").Parse(tmplResetEnumDisabledFields) |
| 51 | + // ParseFiles(path.Join(templatePath)) |
| 52 | + if errTmpl != nil { |
| 53 | + log.Panic(errTmpl) |
| 54 | + } |
| 55 | + |
| 56 | + generatedFileName := strings.TrimSuffix(fileName, ".go") + "_prepare_for_runtime.go" |
| 57 | + _ = os.Truncate(generatedFileName, 0) |
| 58 | + file, errFile := os.OpenFile(generatedFileName, os.O_CREATE|os.O_WRONLY, 0o600) |
| 59 | + if errFile != nil { |
| 60 | + log.Panic(errFile) |
| 61 | + } |
| 62 | + defer file.Close() |
| 63 | + |
| 64 | + doNotSendDisabledFields := listEmptyDisabledFields(allEnumFields) |
| 65 | + doNotSendEnabledFields := listEmtpyEnabledFields(allEnumFields) |
| 66 | + |
| 67 | + errTmpl = tmpl.Execute(file, map[string]interface{}{ |
| 68 | + "PrepareFieldsForRuntimeAddServer": FuncPrepareFieldsForRuntimeAddServer, |
| 69 | + "DoNotSendDisabledFields": doNotSendDisabledFields, |
| 70 | + "DoNotSendDisabledFieldsFunc": FuncDoNotSendDisabledFields, |
| 71 | + "DoNotSendEnabledFields": doNotSendEnabledFields, |
| 72 | + "DoNotSendEnabledFieldsFunc": FuncDoNotSendEnabledFields, |
| 73 | + "Package": packageName, |
| 74 | + "Licence": args.Licence, |
| 75 | + }) |
| 76 | + if errTmpl != nil { |
| 77 | + log.Panic(errTmpl) |
| 78 | + } |
| 79 | + |
| 80 | + errFmt := fmtFile(generatedFileName) |
| 81 | + if errFmt != nil { |
| 82 | + log.Panic(errFmt) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func enumFieldsEnabledDisabled(filename string) (string, []string) { //nolint:gocognit |
| 88 | + var fieldsWithComment []string |
| 89 | + f, err := decorator.ParseFile(token.NewFileSet(), filename, nil, 0) |
| 90 | + if err != nil { |
| 91 | + log.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + for _, decl := range f.Decls { |
| 95 | + if genDecl, ok := decl.(*dst.GenDecl); ok && genDecl.Tok == token.TYPE { |
| 96 | + for _, spec := range genDecl.Specs { |
| 97 | + if typeSpec, ok := spec.(*dst.TypeSpec); ok { |
| 98 | + if structType, ok := typeSpec.Type.(*dst.StructType); ok { |
| 99 | + for _, field := range structType.Fields.List { |
| 100 | + comments := field.Decorations().Start.All() |
| 101 | + |
| 102 | + for _, comment := range comments { |
| 103 | + if comment == commentEnumFieldsToReset { |
| 104 | + // Add field name to the list |
| 105 | + if len(field.Names) > 0 { |
| 106 | + fieldsWithComment = append(fieldsWithComment, field.Names[0].Name) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return f.Name.Name, fieldsWithComment |
| 117 | +} |
0 commit comments