Skip to content
Open
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
3 changes: 2 additions & 1 deletion gojson/gojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion json-to-array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion json-to-struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 17 additions & 7 deletions json-to-struct_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gojson

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -12,31 +13,31 @@ 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)
}
}

// 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)
}
}

// 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)
}
}

// 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)
}
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
}