|
| 1 | +// Command gentypes generates Go type declarations from a JSON Schema. |
| 2 | +// |
| 3 | +// Usage: |
| 4 | +// |
| 5 | +// gentypes [flags] [schema-file] |
| 6 | +// |
| 7 | +// The schema is read from the file argument, or from standard input when no |
| 8 | +// file is given. Generated Go source is written to standard output, or to |
| 9 | +// the file named by -o. |
| 10 | +// |
| 11 | +// As a go tool: |
| 12 | +// |
| 13 | +// go tool gentypes -package models -o models.go schema.json |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "flag" |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/go-rotini/jsonschema" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + os.Exit(run(os.Args[1:], os.Stdin, os.Stdout, os.Stderr)) |
| 27 | +} |
| 28 | + |
| 29 | +// run is the testable core of main: it parses args, generates types, and |
| 30 | +// returns the process exit code. |
| 31 | +func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { |
| 32 | + flags := flag.NewFlagSet("gentypes", flag.ContinueOnError) |
| 33 | + flags.SetOutput(stderr) |
| 34 | + pkg := flags.String("package", "models", "package name for the generated file") |
| 35 | + root := flags.String("root", "", `name of the root type (default: schema "title", else "Root")`) |
| 36 | + outPath := flags.String("o", "", "write output to this file instead of stdout") |
| 37 | + flags.Usage = func() { |
| 38 | + fmt.Fprintln(stderr, "usage: gentypes [flags] [schema-file]") |
| 39 | + fmt.Fprintln(stderr, "reads a JSON Schema (from schema-file or stdin) and writes Go types") |
| 40 | + flags.PrintDefaults() |
| 41 | + } |
| 42 | + if err := flags.Parse(args); err != nil { |
| 43 | + return 2 |
| 44 | + } |
| 45 | + |
| 46 | + schemaJSON, err := readSchema(flags.Arg(0), stdin) |
| 47 | + if err != nil { |
| 48 | + fmt.Fprintln(stderr, "gentypes:", err) |
| 49 | + return 1 |
| 50 | + } |
| 51 | + |
| 52 | + opts := []jsonschema.GoOption{jsonschema.WithGoPackage(*pkg)} |
| 53 | + if *root != "" { |
| 54 | + opts = append(opts, jsonschema.WithGoRootType(*root)) |
| 55 | + } |
| 56 | + src, err := jsonschema.GenerateGo(schemaJSON, opts...) |
| 57 | + if err != nil { |
| 58 | + fmt.Fprintln(stderr, "gentypes:", err) |
| 59 | + return 1 |
| 60 | + } |
| 61 | + |
| 62 | + if err := writeOutput(*outPath, src, stdout); err != nil { |
| 63 | + fmt.Fprintln(stderr, "gentypes:", err) |
| 64 | + return 1 |
| 65 | + } |
| 66 | + return 0 |
| 67 | +} |
| 68 | + |
| 69 | +// readSchema reads schema bytes from path, or from stdin when path is empty. |
| 70 | +func readSchema(path string, stdin io.Reader) ([]byte, error) { |
| 71 | + if path == "" { |
| 72 | + data, err := io.ReadAll(stdin) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("read stdin: %w", err) |
| 75 | + } |
| 76 | + return data, nil |
| 77 | + } |
| 78 | + data, err := os.ReadFile(path) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("read schema file: %w", err) |
| 81 | + } |
| 82 | + return data, nil |
| 83 | +} |
| 84 | + |
| 85 | +// writeOutput writes src to path, or to stdout when path is empty. |
| 86 | +func writeOutput(path string, src []byte, stdout io.Writer) error { |
| 87 | + if path == "" { |
| 88 | + if _, err := stdout.Write(src); err != nil { |
| 89 | + return fmt.Errorf("write stdout: %w", err) |
| 90 | + } |
| 91 | + return nil |
| 92 | + } |
| 93 | + if err := os.WriteFile(path, src, 0o600); err != nil { |
| 94 | + return fmt.Errorf("write output file: %w", err) |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
0 commit comments