Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --file-header option #23

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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