|
| 1 | +// flatten generates an optimized Go struct from a source file. |
| 2 | +// |
| 3 | +// Usage: |
| 4 | +// |
| 5 | +// //go:generate go run github.com/cloudquery/codegen/cmd/flatten -source=../api/models.go -from=Finding -to=flatFinding -output=finding_generated.go -package=services -sort |
| 6 | +// //go:generate go run github.com/cloudquery/codegen/cmd/flatten -source=../api/models.go -from=Finding -to=flatFinding -output=finding_generated.go -package=services -sort -extra=AssetType:string:assetType,omitempty |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "flag" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/cloudquery/codegen/structs" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + source := flag.String("source", "", "path to Go source file containing the struct") |
| 21 | + from := flag.String("from", "", "name of the source struct") |
| 22 | + to := flag.String("to", "", "name of the generated struct") |
| 23 | + output := flag.String("output", "", "output filename") |
| 24 | + pkg := flag.String("package", "", "Go package name for the generated file") |
| 25 | + sort := flag.Bool("sort", false, "sort fields: ID first, then alphabetically") |
| 26 | + extra := flag.String("extra", "", "extra fields to prepend (Name:Type:JSONTag)") |
| 27 | + flag.Parse() |
| 28 | + |
| 29 | + if *source == "" || *from == "" || *to == "" || *output == "" { |
| 30 | + flag.Usage() |
| 31 | + log.Fatal("required flags: -source, -from, -to, -output") |
| 32 | + } |
| 33 | + |
| 34 | + cfg := structs.StructConfig{ |
| 35 | + SourceName: *from, |
| 36 | + OutputName: *to, |
| 37 | + OutputFile: filepath.Base(*output), |
| 38 | + ExtraFields: parseExtraFields(*extra), |
| 39 | + } |
| 40 | + |
| 41 | + outputDir := filepath.Dir(*output) |
| 42 | + if outputDir == "" || outputDir == "." { |
| 43 | + outputDir = "." |
| 44 | + } |
| 45 | + |
| 46 | + var opts []structs.Option |
| 47 | + if *pkg != "" { |
| 48 | + opts = append(opts, structs.WithPackageName(*pkg)) |
| 49 | + } |
| 50 | + if *sort { |
| 51 | + opts = append(opts, structs.WithSortFields()) |
| 52 | + } |
| 53 | + |
| 54 | + if err := structs.Flatten(*source, []structs.StructConfig{cfg}, outputDir, opts...); err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func parseExtraFields(s string) []structs.Field { |
| 60 | + if s == "" { |
| 61 | + return nil |
| 62 | + } |
| 63 | + parts := strings.SplitN(s, ":", 3) |
| 64 | + if len(parts) != 3 { |
| 65 | + log.Fatalf("invalid -extra %q: expected Name:Type:JSONTag", s) |
| 66 | + } |
| 67 | + return []structs.Field{{ |
| 68 | + Name: strings.TrimSpace(parts[0]), |
| 69 | + Type: strings.TrimSpace(parts[1]), |
| 70 | + JSONTag: strings.TrimSpace(parts[2]), |
| 71 | + }} |
| 72 | +} |
| 73 | + |
| 74 | +func init() { |
| 75 | + flag.Usage = func() { |
| 76 | + fmt.Fprintf(flag.CommandLine.Output(), "Usage: flatten [flags]\n\nGenerates an optimized Go struct with nested fields replaced by map[string]any.\n\nFlags:\n") |
| 77 | + flag.PrintDefaults() |
| 78 | + } |
| 79 | +} |
0 commit comments