From b7f3a3bf60519808895f82e3f7e3b2eb65b87281 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 20 Jun 2020 22:18:58 +0500 Subject: [PATCH 1/3] generate "code generated" comment. Closes #47 --- gojson/gojson.go | 3 ++- json-to-struct.go | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) 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-struct.go b/json-to-struct.go index 811ee71..9c2ad3c 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -198,7 +198,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 bool, convertFloats bool, comment bool) ([]byte, error) { var subStructMap map[string]string = nil if subStruct { subStructMap = make(map[string]string) @@ -246,6 +246,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 = "// Code generated by gojson. DO NOT EDIT.\n\n\n" + src + } + formatted, err := format.Source([]byte(src)) if err != nil { err = fmt.Errorf("error formatting: %s, was formatting\n%s", err, src) From bf6b812f582fb60fabf8e788863e4b426337583c Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 20 Jun 2020 22:18:58 +0500 Subject: [PATCH 2/3] generate "code generated" comment. Closes #47 --- json-to-array_test.go | 2 +- json-to-struct.go | 6 ++++-- json-to-struct_test.go | 24 +++++++++++++++++------- 3 files changed, 22 insertions(+), 10 deletions(-) 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 9c2ad3c..9e0ee8c 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\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, comment 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) @@ -247,7 +249,7 @@ func Generate(input io.Reader, parser Parser, structName, pkgName string, tags [ } if comment { - src = "// Code generated by gojson. DO NOT EDIT.\n\n\n" + src + src = codeGeneratedComment + src } formatted, err := format.Source([]byte(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") + } +} From a055bf7e60728a23a8860225a43492a8afcaebe5 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 20 Jun 2020 22:18:58 +0500 Subject: [PATCH 3/3] generate "code generated" comment. Closes #47 --- json-to-struct.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-to-struct.go b/json-to-struct.go index 9e0ee8c..0d59c40 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -114,7 +114,7 @@ import ( var ForceFloats bool -const codeGeneratedComment = "// Code generated by gojson. DO NOT EDIT.\n\n\n" +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.