Skip to content

Commit

Permalink
Merge pull request #23 from twpayne/file-header
Browse files Browse the repository at this point in the history
feat: add --file-header option
  • Loading branch information
twpayne authored Sep 20, 2024
2 parents a9f82cf + 1cf8a75 commit e8bee49
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/gojsonstruct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
abbreviations = pflag.String("abbreviations", "", "comma-separated list of extra abbreviations")
format = pflag.String("format", "json", "format (json or yaml)")
decompress = pflag.Bool("z", false, "decompress input with gzip")
fileHeader = pflag.String("file-header", "", "file header")
omitEmptyTags = pflag.String("omitempty-tags", "auto", "generate ,omitempty tags (never, always, or auto)")
packageComment = pflag.String("package-comment", "", "package comment")
packageName = pflag.String("package-name", "main", "package name")
Expand All @@ -40,6 +41,7 @@ func run() error {
pflag.Parse()

options := []jsonstruct.GeneratorOption{
jsonstruct.WithFileHeader(*fileHeader),
jsonstruct.WithOmitEmptyTags(omitEmptyTagsType[*omitEmptyTags]),
jsonstruct.WithSkipUnparsableProperties(*skipUnparsableProperties),
jsonstruct.WithStringTags(*stringTags),
Expand Down
11 changes: 11 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Generator struct {
abbreviations map[string]bool
exportNameFunc ExportNameFunc
exportRenames map[string]string
fileHeader string
goFormat bool
imports map[string]struct{}
intType string
Expand Down Expand Up @@ -76,6 +77,13 @@ func WithExtraAbbreviations(abbreviations ...string) GeneratorOption {
}
}

// WithFileHeader sets the file header.
func WithFileHeader(fileHeader string) GeneratorOption {
return func(g *Generator) {
g.fileHeader = fileHeader
}
}

// WithGoFormat sets whether the output is should be formatted with go fmt.
func WithGoFormat(goFormat bool) GeneratorOption {
return func(g *Generator) {
Expand Down Expand Up @@ -219,6 +227,9 @@ func NewGenerator(options ...GeneratorOption) *Generator {
func (g *Generator) Generate() ([]byte, error) {
buffer := &bytes.Buffer{}
buffer.Grow(65536)
if g.fileHeader != "" {
fmt.Fprintf(buffer, "%s\n\n", g.fileHeader)
}
if g.packageComment != "" {
fmt.Fprintf(buffer, "// %s\n", g.packageComment)
}
Expand Down
17 changes: 17 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,20 @@ func TestObserveJSONGoCode(t *testing.T) {
"// MyType is my type.\n" +
"type MyType bool\n",
},
{
name: "file_header",
json: "" +
`true`,
generatorOptions: []GeneratorOption{
WithFileHeader("// File header."),
},
expectedGoCodeStr: "" +
"// File header.\n" +
"\n" +
"package main\n" +
"\n" +
"type T bool\n",
},
{
name: "time",
json: `"1985-04-12T23:20:50.52Z"`,
Expand Down Expand Up @@ -1420,6 +1434,7 @@ func ExampleGenerator_ObserveJSONFile() {

func ExampleGenerator_ObserveYAMLFile() {
generator := NewGenerator(
WithFileHeader("// Code generated by gojsonstruct. DO NOT EDIT."),
WithPackageName("mypackage"),
WithTypeName("MyType"),
)
Expand All @@ -1433,6 +1448,8 @@ func ExampleGenerator_ObserveYAMLFile() {
os.Stdout.Write(data)

// Output:
// // Code generated by gojsonstruct. DO NOT EDIT.
//
// package mypackage
//
// type MyType struct {
Expand Down

0 comments on commit e8bee49

Please sign in to comment.