From 45ec9dac1968e61aef8d33b70523d3bdd4026295 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 17:58:57 -0700 Subject: [PATCH 01/19] optional package name --- gojson/gojson.go | 2 +- json-to-struct.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gojson/gojson.go b/gojson/gojson.go index 04ba71c..997f13c 100644 --- a/gojson/gojson.go +++ b/gojson/gojson.go @@ -55,7 +55,7 @@ import ( var ( name = flag.String("name", "Foo", "the name of the struct") - pkg = flag.String("pkg", "main", "the name of the package for the generated code") + pkg = flag.String("pkg", "", "the name of the package for the generated code") inputName = flag.String("input", "", "the name of the input file containing JSON (if input not provided via STDIN)") outputName = flag.String("o", "", "the name of the file to write the output to (outputs to STDOUT by default)") ) diff --git a/json-to-struct.go b/json-to-struct.go index 125bfd5..2ff24c0 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -148,6 +148,10 @@ var commonInitialisms = map[string]bool{ // Given a JSON string representation of an object and a name structName, // attemp to generate a struct definition func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { + pkg := "" + if pkgName != "" { + pkg = fmt.Sprintf("package %s\n", pkgName) + } var iresult interface{} var result map[string]interface{} if err := json.NewDecoder(input).Decode(&iresult); err != nil { @@ -164,8 +168,8 @@ func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { return nil, fmt.Errorf("empty array") } case []interface{}: - src := fmt.Sprintf("package %s\n\ntype %s %s\n", - pkgName, + src := fmt.Sprintf("%s\ntype %s %s\n", + pkg, structName, "[]interface{}") return []byte(src), nil @@ -174,8 +178,8 @@ func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { return nil, fmt.Errorf("unexpected type: %T", iresult) } - src := fmt.Sprintf("package %s\ntype %s %s}", - pkgName, + src := fmt.Sprintf("%stype %s %s}", + pkg, structName, generateTypes(result, 0)) formatted, err := format.Source([]byte(src)) From c781f094e27c3a18ae5e6382728d40c25b8ef0ec Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 19:45:08 -0700 Subject: [PATCH 02/19] restructured packages --- Makefile | 7 +++---- json-to-array_test.go | 2 +- json-to-struct.go | 4 +++- json-to-struct_test.go | 4 ++-- gojson/gojson.go => main.go | 2 -- .../expected_output_array_test.go | 0 .../expected_output_test.go | 0 7 files changed, 9 insertions(+), 10 deletions(-) rename gojson/gojson.go => main.go (98%) rename expected_output_array_test.go => testdata/expected_output_array_test.go (100%) rename expected_output_test.go => testdata/expected_output_test.go (100%) diff --git a/Makefile b/Makefile index b86fd5c..6fa7ac4 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ -build/gojson: format test - mkdir -p build - go build -o build/gojson ./gojson +build: format test + go build -o build/gojson test: go test -v format: - gofmt -w -e -s -l *.go **/*.go + gofmt -w -e -s -l *.go clean: rm -rf build diff --git a/json-to-array_test.go b/json-to-array_test.go index 39328ff..c1a7078 100644 --- a/json-to-array_test.go +++ b/json-to-array_test.go @@ -1,4 +1,4 @@ -package json2struct +package main import ( "os" diff --git a/json-to-struct.go b/json-to-struct.go index 2ff24c0..62b7095 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -94,7 +94,7 @@ // Watchers float64 `json:"watchers"` // WatchersCount float64 `json:"watchers_count"` // } -package json2struct +package main import ( "encoding/json" @@ -149,7 +149,9 @@ var commonInitialisms = map[string]bool{ // attemp to generate a struct definition func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { pkg := "" + fmt.Printf("pkg: %s\n", pkgName) if pkgName != "" { + fmt.Printf("pkg != \"\"\n") pkg = fmt.Sprintf("package %s\n", pkgName) } var iresult interface{} diff --git a/json-to-struct_test.go b/json-to-struct_test.go index 0b50ac5..c71399b 100644 --- a/json-to-struct_test.go +++ b/json-to-struct_test.go @@ -1,4 +1,4 @@ -package json2struct +package main import ( "io/ioutil" @@ -47,7 +47,7 @@ func TestExample(t *testing.T) { t.Error("error opening example.json", err) } - expected, err := ioutil.ReadFile("expected_output_test.go") + expected, err := ioutil.ReadFile("testdata/expected_output_test.go") if err != nil { t.Error("error reading expected_output_test.go", err) } diff --git a/gojson/gojson.go b/main.go similarity index 98% rename from gojson/gojson.go rename to main.go index 997f13c..2b670e4 100644 --- a/gojson/gojson.go +++ b/main.go @@ -49,8 +49,6 @@ import ( "io/ioutil" "log" "os" - - . "github.com/ChimeraCoder/gojson" ) var ( diff --git a/expected_output_array_test.go b/testdata/expected_output_array_test.go similarity index 100% rename from expected_output_array_test.go rename to testdata/expected_output_array_test.go diff --git a/expected_output_test.go b/testdata/expected_output_test.go similarity index 100% rename from expected_output_test.go rename to testdata/expected_output_test.go From 8b494508c47961885954a24577e2b401fa5281e0 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 19:54:35 -0700 Subject: [PATCH 03/19] removed debug printing --- json-to-struct.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/json-to-struct.go b/json-to-struct.go index 62b7095..f2a00a2 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -149,9 +149,7 @@ var commonInitialisms = map[string]bool{ // attemp to generate a struct definition func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { pkg := "" - fmt.Printf("pkg: %s\n", pkgName) if pkgName != "" { - fmt.Printf("pkg != \"\"\n") pkg = fmt.Sprintf("package %s\n", pkgName) } var iresult interface{} From 2491459da403a0797b543c325794306258e3b53b Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 19:57:27 -0700 Subject: [PATCH 04/19] renamed main to gojson.go --- main.go => gojson.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main.go => gojson.go (100%) diff --git a/main.go b/gojson.go similarity index 100% rename from main.go rename to gojson.go From 437857585d5302fde9961e0c43e7f0198ab8f067 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 20:07:24 -0700 Subject: [PATCH 05/19] update test to compare bytes, instead of string conversion --- json-to-struct_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/json-to-struct_test.go b/json-to-struct_test.go index c71399b..093be05 100644 --- a/json-to-struct_test.go +++ b/json-to-struct_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "io/ioutil" "os" "strings" @@ -56,9 +57,8 @@ func TestExample(t *testing.T) { if err != nil { t.Error(err) } - sactual, sexpected := string(actual), string(expected) - if sactual != sexpected { - t.Errorf("'%s' (expected) != '%s' (actual)", sexpected, sactual) + if !bytes.Equal(actual, expected) { + t.Errorf("'%s' (expected) != '%s' (actual)", expected, actual) } } From 5d4d52275fdc6bdeaa522c71375aeb4f9e4a4081 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 20:23:38 -0700 Subject: [PATCH 06/19] update readme to use makefile, and go install --- README.md | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 08b111f..fe54c5c 100644 --- a/README.md +++ b/README.md @@ -100,23 +100,15 @@ type Repository struct { } ``` -CLI Installation +Installation ---------------- ```sh -$ go get github.com/ChimeraCoder/gojson/gojson +$ go install github.com/ChimeraCoder/gojson ``` Assuming `$GOPATH/bin` is in your `PATH`, you can now invoke `gojson` directly. - -API Installation ----------------- - -```sh -$ go get github.com/ChimeraCoder/gojson -``` - Development ----------- @@ -126,24 +118,25 @@ $ cd gojson $ go test ``` -**Building CLI** +**Building** ``` -$ go build -o _build/gojson ./gojson +$ make build ``` -**Installing CLI** +**Formatting** ``` -$ go install ./gojson +$ make format ``` -**Formatting** +**Testing** ``` -$ gofmt -w -e -s -l . +$ make test ``` + Related Work ------------ From f895092cf00d6c8d08d3f64da3c467e08c31ad98 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 21:31:06 -0700 Subject: [PATCH 07/19] started doc.go --- doc.go | 125 ++++++++++++++++++++++++++++++++++++++++++++++ gojson.go | 42 ---------------- json-to-struct.go | 96 ----------------------------------- 3 files changed, 125 insertions(+), 138 deletions(-) create mode 100644 doc.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..a592d65 --- /dev/null +++ b/doc.go @@ -0,0 +1,125 @@ +/* +GoJson generates go struct defintions from JSON documents + +It reads from stdin and prints to stdout. + +Usage: + gojson [flags] + +The flags are: + -name + the name of the struct + -pkg + the name of the package for the generated code + -inputName + the name of the input file containing JSON (if input not provided via STDIN) + -outputName + the name of the file to write the output to (outputs to STDOUT by default) + +Examples + +Convert a simple json string + + echo '{"hello":"world"}' | gojson + +Output: + + type Foo struct { + Hello string `json:"hello"` + } + +Convert resulting json of an endpoint, +giving the resulting struct the name "Repository" + + curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository + +Output: + + type Repository struct { + ArchiveURL string `json:"archive_url"` + AssigneesURL string `json:"assignees_url"` + BlobsURL string `json:"blobs_url"` + BranchesURL string `json:"branches_url"` + CloneURL string `json:"clone_url"` + CollaboratorsURL string `json:"collaborators_url"` + CommentsURL string `json:"comments_url"` + CommitsURL string `json:"commits_url"` + CompareURL string `json:"compare_url"` + ContentsURL string `json:"contents_url"` + ContributorsURL string `json:"contributors_url"` + CreatedAt string `json:"created_at"` + DefaultBranch string `json:"default_branch"` + Description string `json:"description"` + DownloadsURL string `json:"downloads_url"` + EventsURL string `json:"events_url"` + Fork bool `json:"fork"` + Forks float64 `json:"forks"` + ForksCount float64 `json:"forks_count"` + ForksURL string `json:"forks_url"` + FullName string `json:"full_name"` + GitCommitsURL string `json:"git_commits_url"` + GitRefsURL string `json:"git_refs_url"` + GitTagsURL string `json:"git_tags_url"` + GitURL string `json:"git_url"` + HasDownloads bool `json:"has_downloads"` + HasIssues bool `json:"has_issues"` + HasWiki bool `json:"has_wiki"` + Homepage interface{} `json:"homepage"` + HooksURL string `json:"hooks_url"` + HtmlURL string `json:"html_url"` + ID float64 `json:"id"` + IssueCommentURL string `json:"issue_comment_url"` + IssueEventsURL string `json:"issue_events_url"` + IssuesURL string `json:"issues_url"` + KeysURL string `json:"keys_url"` + LabelsURL string `json:"labels_url"` + Language string `json:"language"` + LanguagesURL string `json:"languages_url"` + MasterBranch string `json:"master_branch"` + MergesURL string `json:"merges_url"` + MilestonesURL string `json:"milestones_url"` + MirrorURL interface{} `json:"mirror_url"` + Name string `json:"name"` + NetworkCount float64 `json:"network_count"` + NotificationsURL string `json:"notifications_url"` + OpenIssues float64 `json:"open_issues"` + OpenIssuesCount float64 `json:"open_issues_count"` + Owner struct { + AvatarURL string `json:"avatar_url"` + EventsURL string `json:"events_url"` + FollowersURL string `json:"followers_url"` + FollowingURL string `json:"following_url"` + GistsURL string `json:"gists_url"` + GravatarID string `json:"gravatar_id"` + HtmlURL string `json:"html_url"` + ID float64 `json:"id"` + Login string `json:"login"` + OrganizationsURL string `json:"organizations_url"` + ReceivedEventsURL string `json:"received_events_url"` + ReposURL string `json:"repos_url"` + SiteAdmin bool `json:"site_admin"` + StarredURL string `json:"starred_url"` + SubscriptionsURL string `json:"subscriptions_url"` + Type string `json:"type"` + URL string `json:"url"` + } `json:"owner"` + Private bool `json:"private"` + PullsURL string `json:"pulls_url"` + PushedAt string `json:"pushed_at"` + Size float64 `json:"size"` + SshURL string `json:"ssh_url"` + StargazersURL string `json:"stargazers_url"` + StatusesURL string `json:"statuses_url"` + SubscribersURL string `json:"subscribers_url"` + SubscriptionURL string `json:"subscription_url"` + SvnURL string `json:"svn_url"` + TagsURL string `json:"tags_url"` + TeamsURL string `json:"teams_url"` + TreesURL string `json:"trees_url"` + UpdatedAt string `json:"updated_at"` + URL string `json:"url"` + Watchers float64 `json:"watchers"` + WatchersCount float64 `json:"watchers_count"` + } +*/ +package main diff --git a/gojson.go b/gojson.go index 2b670e4..36a92db 100644 --- a/gojson.go +++ b/gojson.go @@ -1,45 +1,3 @@ -// gojson generates go struct defintions from JSON documents -// -// Reads from stdin and prints to stdout -// -// Example: -// curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository -// -// Output: -// package main -// -// type User struct { -// AvatarURL string `json:"avatar_url"` -// Bio interface{} `json:"bio"` -// Blog string `json:"blog"` -// Company string `json:"company"` -// CreatedAt string `json:"created_at"` -// Email string `json:"email"` -// EventsURL string `json:"events_url"` -// Followers float64 `json:"followers"` -// FollowersURL string `json:"followers_url"` -// Following float64 `json:"following"` -// FollowingURL string `json:"following_url"` -// GistsURL string `json:"gists_url"` -// GravatarID string `json:"gravatar_id"` -// Hireable bool `json:"hireable"` -// HtmlURL string `json:"html_url"` -// ID float64 `json:"id"` -// Location string `json:"location"` -// Login string `json:"login"` -// Name string `json:"name"` -// OrganizationsURL string `json:"organizations_url"` -// PublicGists float64 `json:"public_gists"` -// PublicRepos float64 `json:"public_repos"` -// ReceivedEventsURL string `json:"received_events_url"` -// ReposURL string `json:"repos_url"` -// StarredURL string `json:"starred_url"` -// SubscriptionsURL string `json:"subscriptions_url"` -// Type string `json:"type"` -// UpdatedAt string `json:"updated_at"` -// URL string `json:"url"` -// } - package main import ( diff --git a/json-to-struct.go b/json-to-struct.go index f2a00a2..2feb65c 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -1,99 +1,3 @@ -// gojson generates go struct defintions from JSON documents -// -// Reads from stdin and prints to stdout -// -// Example: -// curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository -// -// Output: -// package main -// -// type Repository struct { -// ArchiveURL string `json:"archive_url"` -// AssigneesURL string `json:"assignees_url"` -// BlobsURL string `json:"blobs_url"` -// BranchesURL string `json:"branches_url"` -// CloneURL string `json:"clone_url"` -// CollaboratorsURL string `json:"collaborators_url"` -// CommentsURL string `json:"comments_url"` -// CommitsURL string `json:"commits_url"` -// CompareURL string `json:"compare_url"` -// ContentsURL string `json:"contents_url"` -// ContributorsURL string `json:"contributors_url"` -// CreatedAt string `json:"created_at"` -// DefaultBranch string `json:"default_branch"` -// Description string `json:"description"` -// DownloadsURL string `json:"downloads_url"` -// EventsURL string `json:"events_url"` -// Fork bool `json:"fork"` -// Forks float64 `json:"forks"` -// ForksCount float64 `json:"forks_count"` -// ForksURL string `json:"forks_url"` -// FullName string `json:"full_name"` -// GitCommitsURL string `json:"git_commits_url"` -// GitRefsURL string `json:"git_refs_url"` -// GitTagsURL string `json:"git_tags_url"` -// GitURL string `json:"git_url"` -// HasDownloads bool `json:"has_downloads"` -// HasIssues bool `json:"has_issues"` -// HasWiki bool `json:"has_wiki"` -// Homepage interface{} `json:"homepage"` -// HooksURL string `json:"hooks_url"` -// HtmlURL string `json:"html_url"` -// ID float64 `json:"id"` -// IssueCommentURL string `json:"issue_comment_url"` -// IssueEventsURL string `json:"issue_events_url"` -// IssuesURL string `json:"issues_url"` -// KeysURL string `json:"keys_url"` -// LabelsURL string `json:"labels_url"` -// Language string `json:"language"` -// LanguagesURL string `json:"languages_url"` -// MasterBranch string `json:"master_branch"` -// MergesURL string `json:"merges_url"` -// MilestonesURL string `json:"milestones_url"` -// MirrorURL interface{} `json:"mirror_url"` -// Name string `json:"name"` -// NetworkCount float64 `json:"network_count"` -// NotificationsURL string `json:"notifications_url"` -// OpenIssues float64 `json:"open_issues"` -// OpenIssuesCount float64 `json:"open_issues_count"` -// Owner struct { -// AvatarURL string `json:"avatar_url"` -// EventsURL string `json:"events_url"` -// FollowersURL string `json:"followers_url"` -// FollowingURL string `json:"following_url"` -// GistsURL string `json:"gists_url"` -// GravatarID string `json:"gravatar_id"` -// HtmlURL string `json:"html_url"` -// ID float64 `json:"id"` -// Login string `json:"login"` -// OrganizationsURL string `json:"organizations_url"` -// ReceivedEventsURL string `json:"received_events_url"` -// ReposURL string `json:"repos_url"` -// SiteAdmin bool `json:"site_admin"` -// StarredURL string `json:"starred_url"` -// SubscriptionsURL string `json:"subscriptions_url"` -// Type string `json:"type"` -// URL string `json:"url"` -// } ` json:"owner"` -// Private bool `json:"private"` -// PullsURL string `json:"pulls_url"` -// PushedAt string `json:"pushed_at"` -// Size float64 `json:"size"` -// SshURL string `json:"ssh_url"` -// StargazersURL string `json:"stargazers_url"` -// StatusesURL string `json:"statuses_url"` -// SubscribersURL string `json:"subscribers_url"` -// SubscriptionURL string `json:"subscription_url"` -// SvnURL string `json:"svn_url"` -// TagsURL string `json:"tags_url"` -// TeamsURL string `json:"teams_url"` -// TreesURL string `json:"trees_url"` -// UpdatedAt string `json:"updated_at"` -// URL string `json:"url"` -// Watchers float64 `json:"watchers"` -// WatchersCount float64 `json:"watchers_count"` -// } package main import ( From c24711f8ea0ce1574ab53be9e0c66317bc834859 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 21:42:18 -0700 Subject: [PATCH 08/19] godoc example --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index fe54c5c..7e6a08a 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,17 @@ $ make test ``` +Development +----------- +Use godoc to see further documentation + +``` +godoc -http=:6060 +``` + +open browser to http://localhost:6060/pkg/github.com/ChimeraCoder/gojson/ + + Related Work ------------ From 905466c50f346e2c3c3b467227beedbe078e4b25 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 10 Jun 2015 21:44:12 -0700 Subject: [PATCH 09/19] update development to documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e6a08a..f834fcb 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ $ make test ``` -Development +Documentation ----------- Use godoc to see further documentation From afba3bd1ad4f7b700d5b55c94e4a04fe52be8753 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Thu, 11 Jun 2015 08:23:10 -0700 Subject: [PATCH 10/19] changed install to get --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f834fcb..843ae0b 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Installation ---------------- ```sh -$ go install github.com/ChimeraCoder/gojson +$ go get github.com/ChimeraCoder/gojson ``` Assuming `$GOPATH/bin` is in your `PATH`, you can now invoke `gojson` directly. From c7141f1809f72ee8ca41f17eb1de214c0eadf575 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Thu, 11 Jun 2015 08:36:57 -0700 Subject: [PATCH 11/19] update package name to gojson --- doc.go | 2 +- gojson.go | 2 +- json-to-array_test.go | 2 +- json-to-struct.go | 2 +- json-to-struct_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc.go b/doc.go index a592d65..347fe9e 100644 --- a/doc.go +++ b/doc.go @@ -122,4 +122,4 @@ Output: WatchersCount float64 `json:"watchers_count"` } */ -package main +package gojson diff --git a/gojson.go b/gojson.go index 36a92db..ead6298 100644 --- a/gojson.go +++ b/gojson.go @@ -1,4 +1,4 @@ -package main +package gojson import ( "flag" diff --git a/json-to-array_test.go b/json-to-array_test.go index c1a7078..5585b0b 100644 --- a/json-to-array_test.go +++ b/json-to-array_test.go @@ -1,4 +1,4 @@ -package main +package gojson import ( "os" diff --git a/json-to-struct.go b/json-to-struct.go index 2feb65c..7c2a92b 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -1,4 +1,4 @@ -package main +package gojson import ( "encoding/json" diff --git a/json-to-struct_test.go b/json-to-struct_test.go index 093be05..7c7cdf5 100644 --- a/json-to-struct_test.go +++ b/json-to-struct_test.go @@ -1,4 +1,4 @@ -package main +package gojson import ( "bytes" From ddd4fd157ef2a9704a0595dd4d82ad37e2c0deff Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 19:40:42 -0700 Subject: [PATCH 12/19] WIP: Restructured to have generator subpackage. Renamed testdata input and output. --- Makefile | 2 +- doc.go | 2 +- json-to-array_test.go => generator/json-to-array_test.go | 4 ++-- json-to-struct.go => generator/json-to-struct.go | 2 +- json-to-struct_test.go => generator/json-to-struct_test.go | 6 +++--- gojson.go | 6 ++++-- testdata/{expected_output_array_test.go => array.golden} | 0 example_array.json => testdata/array.input | 0 testdata/{expected_output_test.go => struct.golden} | 0 example.json => testdata/struct.input | 0 10 files changed, 12 insertions(+), 10 deletions(-) rename json-to-array_test.go => generator/json-to-array_test.go (89%) rename json-to-struct.go => generator/json-to-struct.go (99%) rename json-to-struct_test.go => generator/json-to-struct_test.go (94%) rename testdata/{expected_output_array_test.go => array.golden} (100%) rename example_array.json => testdata/array.input (100%) rename testdata/{expected_output_test.go => struct.golden} (100%) rename example.json => testdata/struct.input (100%) diff --git a/Makefile b/Makefile index 6fa7ac4..be71201 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build: format test go build -o build/gojson test: - go test -v + go test -v ./... format: gofmt -w -e -s -l *.go diff --git a/doc.go b/doc.go index 347fe9e..a592d65 100644 --- a/doc.go +++ b/doc.go @@ -122,4 +122,4 @@ Output: WatchersCount float64 `json:"watchers_count"` } */ -package gojson +package main diff --git a/json-to-array_test.go b/generator/json-to-array_test.go similarity index 89% rename from json-to-array_test.go rename to generator/json-to-array_test.go index 5585b0b..09645b6 100644 --- a/json-to-array_test.go +++ b/generator/json-to-array_test.go @@ -1,4 +1,4 @@ -package gojson +package generator import ( "os" @@ -7,7 +7,7 @@ import ( // Test example document func TestExampleArray(t *testing.T) { - i, err := os.Open("example_array.json") + i, err := os.Open("../testdata/array.input") if err != nil { t.Error("error opening example.json", err) } diff --git a/json-to-struct.go b/generator/json-to-struct.go similarity index 99% rename from json-to-struct.go rename to generator/json-to-struct.go index 7c2a92b..037b750 100644 --- a/json-to-struct.go +++ b/generator/json-to-struct.go @@ -1,4 +1,4 @@ -package gojson +package generator import ( "encoding/json" diff --git a/json-to-struct_test.go b/generator/json-to-struct_test.go similarity index 94% rename from json-to-struct_test.go rename to generator/json-to-struct_test.go index 7c7cdf5..015d7b4 100644 --- a/json-to-struct_test.go +++ b/generator/json-to-struct_test.go @@ -1,4 +1,4 @@ -package gojson +package generator import ( "bytes" @@ -43,12 +43,12 @@ func TestInvalidFieldChars(t *testing.T) { // Test example document func TestExample(t *testing.T) { - i, err := os.Open("example.json") + i, err := os.Open("../testdata/struct.input") if err != nil { t.Error("error opening example.json", err) } - expected, err := ioutil.ReadFile("testdata/expected_output_test.go") + expected, err := ioutil.ReadFile("../testdata/struct.golden") if err != nil { t.Error("error reading expected_output_test.go", err) } diff --git a/gojson.go b/gojson.go index ead6298..2cd1865 100644 --- a/gojson.go +++ b/gojson.go @@ -1,4 +1,4 @@ -package gojson +package main import ( "flag" @@ -7,6 +7,8 @@ import ( "io/ioutil" "log" "os" + + "github.com/ChimeraCoder/gojson/generator" ) var ( @@ -36,7 +38,7 @@ func main() { input = f } - if output, err := Generate(input, *name, *pkg); err != nil { + if output, err := generator.Generate(input, *name, *pkg); err != nil { fmt.Fprintln(os.Stderr, "error parsing", err) os.Exit(1) } else { diff --git a/testdata/expected_output_array_test.go b/testdata/array.golden similarity index 100% rename from testdata/expected_output_array_test.go rename to testdata/array.golden diff --git a/example_array.json b/testdata/array.input similarity index 100% rename from example_array.json rename to testdata/array.input diff --git a/testdata/expected_output_test.go b/testdata/struct.golden similarity index 100% rename from testdata/expected_output_test.go rename to testdata/struct.golden diff --git a/example.json b/testdata/struct.input similarity index 100% rename from example.json rename to testdata/struct.input From c7c6b46580c2ad798191990ff6ffe4ea4ffb883d Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 19:46:25 -0700 Subject: [PATCH 13/19] updated makefile to format subdirectories --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index be71201..9c022ba 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ test: go test -v ./... format: - gofmt -w -e -s -l *.go + gofmt -w -e -s -l *.go **/*.go clean: rm -rf build From 9e046ceee95359de84300854048bf4de3a896252 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 19:46:51 -0700 Subject: [PATCH 14/19] re-added default package back in for printing --- generator/json-to-struct.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/generator/json-to-struct.go b/generator/json-to-struct.go index 037b750..ba177cc 100644 --- a/generator/json-to-struct.go +++ b/generator/json-to-struct.go @@ -52,10 +52,6 @@ var commonInitialisms = map[string]bool{ // Given a JSON string representation of an object and a name structName, // attemp to generate a struct definition func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { - pkg := "" - if pkgName != "" { - pkg = fmt.Sprintf("package %s\n", pkgName) - } var iresult interface{} var result map[string]interface{} if err := json.NewDecoder(input).Decode(&iresult); err != nil { @@ -72,8 +68,8 @@ func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { return nil, fmt.Errorf("empty array") } case []interface{}: - src := fmt.Sprintf("%s\ntype %s %s\n", - pkg, + src := fmt.Sprintf("package %s\n\ntype %s %s\n", + pkgName, structName, "[]interface{}") return []byte(src), nil @@ -82,8 +78,8 @@ func Generate(input io.Reader, structName, pkgName string) ([]byte, error) { return nil, fmt.Errorf("unexpected type: %T", iresult) } - src := fmt.Sprintf("%stype %s %s}", - pkg, + src := fmt.Sprintf("package %s\ntype %s %s}", + pkgName, structName, generateTypes(result, 0)) formatted, err := format.Source([]byte(src)) From 48a7904a29f810c4a4bb7c321e3d81fb78a6fb98 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 19:51:08 -0700 Subject: [PATCH 15/19] updated test comment --- testdata/array.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/array.golden b/testdata/array.golden index 0e5cf43..e53a4cb 100644 --- a/testdata/array.golden +++ b/testdata/array.golden @@ -1,6 +1,6 @@ package json2struct -// This is what we would like example_array.json +// This is what we would like array.input // to generate, though for the time being we'll have to settle // for []interface{} // https://github.com/ChimeraCoder/gojson/issues/17 From 9433c4332bf21649efd2756f57c2feb66a35b67d Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 21:04:08 -0700 Subject: [PATCH 16/19] moved executable to cmd/ dir. Moved library to top-level --- Makefile | 6 +++--- README.md | 2 +- gojson.go => cmd/gojson/gojson.go | 4 ++-- doc.go | 2 +- generator/json-to-array_test.go => json-to-array_test.go | 4 ++-- generator/json-to-struct.go => json-to-struct.go | 2 +- generator/json-to-struct_test.go => json-to-struct_test.go | 6 +++--- 7 files changed, 13 insertions(+), 13 deletions(-) rename gojson.go => cmd/gojson/gojson.go (91%) rename generator/json-to-array_test.go => json-to-array_test.go (89%) rename generator/json-to-struct.go => json-to-struct.go (99%) rename generator/json-to-struct_test.go => json-to-struct_test.go (94%) diff --git a/Makefile b/Makefile index 9c022ba..4817f5f 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ build: format test - go build -o build/gojson + go build -o build/gojson cmd/gojson/gojson.go test: - go test -v ./... + go test -v format: - gofmt -w -e -s -l *.go **/*.go + gofmt -w -e -s -l *.go clean: rm -rf build diff --git a/README.md b/README.md index 843ae0b..aca8274 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Installation ---------------- ```sh -$ go get github.com/ChimeraCoder/gojson +$ go get github.com/ChimeraCoder/gojson/... ``` Assuming `$GOPATH/bin` is in your `PATH`, you can now invoke `gojson` directly. diff --git a/gojson.go b/cmd/gojson/gojson.go similarity index 91% rename from gojson.go rename to cmd/gojson/gojson.go index 2cd1865..88a4175 100644 --- a/gojson.go +++ b/cmd/gojson/gojson.go @@ -8,7 +8,7 @@ import ( "log" "os" - "github.com/ChimeraCoder/gojson/generator" + "github.com/ChimeraCoder/gojson" ) var ( @@ -38,7 +38,7 @@ func main() { input = f } - if output, err := generator.Generate(input, *name, *pkg); err != nil { + if output, err := gojson.Generate(input, *name, *pkg); err != nil { fmt.Fprintln(os.Stderr, "error parsing", err) os.Exit(1) } else { diff --git a/doc.go b/doc.go index a592d65..347fe9e 100644 --- a/doc.go +++ b/doc.go @@ -122,4 +122,4 @@ Output: WatchersCount float64 `json:"watchers_count"` } */ -package main +package gojson diff --git a/generator/json-to-array_test.go b/json-to-array_test.go similarity index 89% rename from generator/json-to-array_test.go rename to json-to-array_test.go index 09645b6..5707861 100644 --- a/generator/json-to-array_test.go +++ b/json-to-array_test.go @@ -1,4 +1,4 @@ -package generator +package gojson import ( "os" @@ -7,7 +7,7 @@ import ( // Test example document func TestExampleArray(t *testing.T) { - i, err := os.Open("../testdata/array.input") + i, err := os.Open("testdata/array.input") if err != nil { t.Error("error opening example.json", err) } diff --git a/generator/json-to-struct.go b/json-to-struct.go similarity index 99% rename from generator/json-to-struct.go rename to json-to-struct.go index ba177cc..6f44f64 100644 --- a/generator/json-to-struct.go +++ b/json-to-struct.go @@ -1,4 +1,4 @@ -package generator +package gojson import ( "encoding/json" diff --git a/generator/json-to-struct_test.go b/json-to-struct_test.go similarity index 94% rename from generator/json-to-struct_test.go rename to json-to-struct_test.go index 015d7b4..0ccb81e 100644 --- a/generator/json-to-struct_test.go +++ b/json-to-struct_test.go @@ -1,4 +1,4 @@ -package generator +package gojson import ( "bytes" @@ -43,12 +43,12 @@ func TestInvalidFieldChars(t *testing.T) { // Test example document func TestExample(t *testing.T) { - i, err := os.Open("../testdata/struct.input") + i, err := os.Open("testdata/struct.input") if err != nil { t.Error("error opening example.json", err) } - expected, err := ioutil.ReadFile("../testdata/struct.golden") + expected, err := ioutil.ReadFile("testdata/struct.golden") if err != nil { t.Error("error reading expected_output_test.go", err) } From 430038f1b808658a8ab2030be97f4cad186055d0 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 21:15:13 -0700 Subject: [PATCH 17/19] re-added default package main --- cmd/gojson/gojson.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gojson/gojson.go b/cmd/gojson/gojson.go index 88a4175..ab0b563 100644 --- a/cmd/gojson/gojson.go +++ b/cmd/gojson/gojson.go @@ -13,7 +13,7 @@ import ( var ( name = flag.String("name", "Foo", "the name of the struct") - pkg = flag.String("pkg", "", "the name of the package for the generated code") + pkg = flag.String("pkg", "main", "the name of the package for the generated code") inputName = flag.String("input", "", "the name of the input file containing JSON (if input not provided via STDIN)") outputName = flag.String("o", "", "the name of the file to write the output to (outputs to STDOUT by default)") ) From 91fae3b9b9350cea75e61b8e219af30347f09569 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Tue, 16 Jun 2015 21:17:27 -0700 Subject: [PATCH 18/19] format subdirectories in make format --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4817f5f..7678f67 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ test: go test -v format: - gofmt -w -e -s -l *.go + gofmt -w -e -s -l *.go **.go clean: rm -rf build From 460a56d508cab1f57c6dd1cd2003b474778ea911 Mon Sep 17 00:00:00 2001 From: Trevor Rothaus Date: Wed, 17 Jun 2015 08:12:55 -0700 Subject: [PATCH 19/19] added example file --- doc.go | 10 ---------- example_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 example_test.go diff --git a/doc.go b/doc.go index 347fe9e..db72de9 100644 --- a/doc.go +++ b/doc.go @@ -18,16 +18,6 @@ The flags are: Examples -Convert a simple json string - - echo '{"hello":"world"}' | gojson - -Output: - - type Foo struct { - Hello string `json:"hello"` - } - Convert resulting json of an endpoint, giving the resulting struct the name "Repository" diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..4dd109f --- /dev/null +++ b/example_test.go @@ -0,0 +1,25 @@ +package gojson_test + +import ( + "fmt" + "strings" + + "github.com/ChimeraCoder/gojson" +) + +func ExampleGenerate() { + structName := "test" + pkgName := "main" + input := strings.NewReader(`{"sample":"json"}`) + goStruct, err := gojson.Generate(input, structName, pkgName) + if err != nil { + fmt.Printf("Error generating json: %s", err.Error()) + } + fmt.Printf("%s", goStruct) + // Output: + // package main + // + // type test struct { + // Sample string `json:"sample"` + // } +}