diff --git a/gojson/gojson.go b/gojson/gojson.go index 7539aa5..c3a8eb5 100644 --- a/gojson/gojson.go +++ b/gojson/gojson.go @@ -63,6 +63,7 @@ var ( tags = flag.String("tags", "fmt", "comma seperated list of the tags to put on the struct, default is the same as fmt") forceFloats = flag.Bool("forcefloats", false, "[experimental] force float64 type for integral values") subStruct = flag.Bool("subStruct", false, "create types for sub-structs (default is false)") + comment = flag.Bool("comment", true, "generate comment indicating generated code") ) func main() { @@ -108,7 +109,7 @@ func main() { parser = ParseYaml } - if output, err := Generate(input, parser, *name, *pkg, tagList, *subStruct, convertFloats); err != nil { + if output, err := Generate(input, parser, *name, *pkg, tagList, *subStruct, convertFloats, *comment); err != nil { fmt.Fprintln(os.Stderr, "error parsing", err) os.Exit(1) } else { diff --git a/json-to-array_test.go b/json-to-array_test.go index f94beaa..b90f136 100644 --- a/json-to-array_test.go +++ b/json-to-array_test.go @@ -26,7 +26,7 @@ func TestExampleArray(t *testing.T) { t.Fatalf("error reading example_array.go: %s", err) } - actual, err := Generate(i, ParseJson, "Users", "gojson", []string{"json"}, false, true) + actual, err := Generate(i, ParseJson, "Users", "gojson", []string{"json"}, false, true, false) if err != nil { t.Fatal(err) } diff --git a/json-to-struct.go b/json-to-struct.go index 811ee71..0d59c40 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -114,6 +114,8 @@ import ( var ForceFloats bool +const codeGeneratedComment = "// Code generated by gojson. DO NOT EDIT.\n\n" + // commonInitialisms is a set of common initialisms. // Only add entries that are highly unlikely to be non-initialisms. // For instance, "ID" is fine (Freudian code is rare), but "AND" is not. @@ -198,7 +200,7 @@ func readFile(input io.Reader) ([]byte, error) { } // Generate a struct definition given a JSON string representation of an object and a name structName. -func Generate(input io.Reader, parser Parser, structName, pkgName string, tags []string, subStruct bool, convertFloats bool) ([]byte, error) { +func Generate(input io.Reader, parser Parser, structName, pkgName string, tags []string, subStruct, convertFloats, comment bool) ([]byte, error) { var subStructMap map[string]string = nil if subStruct { subStructMap = make(map[string]string) @@ -246,6 +248,10 @@ func Generate(input io.Reader, parser Parser, structName, pkgName string, tags [ src = fmt.Sprintf("%v\n\ntype %v %v", src, subStructMap[k], k) } + if comment { + src = codeGeneratedComment + src + } + formatted, err := format.Source([]byte(src)) if err != nil { err = fmt.Errorf("error formatting: %s, was formatting\n%s", err, src) diff --git a/json-to-struct_test.go b/json-to-struct_test.go index fe725e2..53988c7 100644 --- a/json-to-struct_test.go +++ b/json-to-struct_test.go @@ -1,6 +1,7 @@ package gojson import ( + "bytes" "io/ioutil" "os" "path/filepath" @@ -12,7 +13,7 @@ import ( // It does not (yet) test for correctness of the end result func TestSimpleJson(t *testing.T) { i := strings.NewReader(`{"foo" : "bar"}`) - if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true); err != nil { + if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true, false); err != nil { t.Error("Generate() error:", err) } } @@ -20,7 +21,7 @@ func TestSimpleJson(t *testing.T) { // TestNullableJson tests that a null JSON value is handled properly func TestNullableJson(t *testing.T) { i := strings.NewReader(`{"foo" : "bar", "baz" : null}`) - if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true); err != nil { + if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true, false); err != nil { t.Error("Generate() error:", err) } } @@ -28,7 +29,7 @@ func TestNullableJson(t *testing.T) { // TestSimpleArray tests that an array without conflicting types is handled correctly func TestSimpleArray(t *testing.T) { i := strings.NewReader(`{"foo" : [{"bar": 24}, {"bar" : 42}]}`) - if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true); err != nil { + if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true, false); err != nil { t.Error("Generate() error:", err) } } @@ -36,7 +37,7 @@ func TestSimpleArray(t *testing.T) { // TestInvalidFieldChars tests that a document with invalid field chars is handled correctly func TestInvalidFieldChars(t *testing.T) { i := strings.NewReader(`{"f.o-o" : 42}`) - if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true); err != nil { + if _, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true, false); err != nil { t.Error("Generate() error:", err) } } @@ -80,7 +81,7 @@ func TestInferFloatInt(t *testing.T) { t.Fatalf("error reading expected_floats.go.out: %s", err) } - actual, err := Generate(f, ParseJson, "Stats", "gojson", []string{"json"}, false, true) + actual, err := Generate(f, ParseJson, "Stats", "gojson", []string{"json"}, false, true, false) if err != nil { t.Error(err) } @@ -104,7 +105,7 @@ func TestYamlNumbers(t *testing.T) { t.Fatalf("error reading expected_numbers.go.out: %s", err) } - actual, err := Generate(f, ParseYaml, "Stats", "gojson", []string{"yaml"}, false, false) + actual, err := Generate(f, ParseYaml, "Stats", "gojson", []string{"yaml"}, false, false, false) if err != nil { t.Error(err) } @@ -126,7 +127,7 @@ func TestExample(t *testing.T) { t.Error("error reading expected_output_test.go", err) } - actual, err := Generate(i, ParseJson, "User", "gojson", []string{"json"}, false, true) + actual, err := Generate(i, ParseJson, "User", "gojson", []string{"json"}, false, true, false) if err != nil { t.Error(err) } @@ -159,3 +160,12 @@ func TestFmtFieldName(t *testing.T) { } } } + +func TestCodeGeneratedComment(t *testing.T) { + i := strings.NewReader(`{"foo" : "bar"}`) + if generated, err := Generate(i, ParseJson, "TestStruct", "gojson", []string{"json"}, false, true, true); err != nil { + t.Error("Generate() error:", err) + } else if !bytes.HasPrefix(generated, []byte(codeGeneratedComment)) { + t.Errorf("error generated code should contain comment indicationg code was generated") + } +}