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
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Repository struct {
CompareURL string `json:"compare_url"`
ContentsURL string `json:"contents_url"`
ContributorsURL string `json:"contributors_url"`
CreatedAt string `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
DefaultBranch string `json:"default_branch"`
Description string `json:"description"`
DownloadsURL string `json:"downloads_url"`
Expand Down Expand Up @@ -80,23 +80,23 @@ type Repository struct {
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"`
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 time.Time `json:"updated_at"`
URL string `json:"url"`
Watchers float64 `json:"watchers"`
WatchersCount float64 `json:"watchers_count"`
}
```

Expand Down
4 changes: 2 additions & 2 deletions examples/example_array.go.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Users []struct {
Bio interface{} `json:"bio"`
Blog string `json:"blog"`
Company interface{} `json:"company"`
CreatedAt string `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
Email interface{} `json:"email"`
EventsURL string `json:"events_url"`
Followers int64 `json:"followers"`
Expand All @@ -29,6 +29,6 @@ type Users []struct {
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
}
4 changes: 2 additions & 2 deletions examples/expected_output_array_test.go.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Users []struct {
Bio interface{} `json:"bio"`
Blog string `json:"blog"`
Company string `json:"company"`
CreatedAt string `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
Email string `json:"email"`
EventsURL string `json:"events_url"`
Followers int64 `json:"followers"`
Expand All @@ -33,6 +33,6 @@ type Users []struct {
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
}
4 changes: 2 additions & 2 deletions examples/expected_output_test.go.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type User struct {
Bio interface{} `json:"bio"`
Blog string `json:"blog"`
Company string `json:"company"`
CreatedAt string `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
Email string `json:"email"`
EventsURL string `json:"events_url"`
Followers int64 `json:"followers"`
Expand All @@ -29,6 +29,6 @@ type User struct {
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
}
4 changes: 2 additions & 2 deletions gojson/gojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Bio interface{} `json:"bio"`
// Blog string `json:"blog"`
// Company string `json:"company"`
// CreatedAt string `json:"created_at"`
// CreatedAt time.Time `json:"created_at"`
// Email string `json:"email"`
// EventsURL string `json:"events_url"`
// Followers float64 `json:"followers"`
Expand All @@ -36,7 +36,7 @@
// StarredURL string `json:"starred_url"`
// SubscriptionsURL string `json:"subscriptions_url"`
// Type string `json:"type"`
// UpdatedAt string `json:"updated_at"`
// UpdatedAt time.Time `json:"updated_at"`
// URL string `json:"url"`
// }

Expand Down
42 changes: 24 additions & 18 deletions json-to-struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// CompareURL string `json:"compare_url"`
// ContentsURL string `json:"contents_url"`
// ContributorsURL string `json:"contributors_url"`
// CreatedAt string `json:"created_at"`
// CreatedAt time.Time `json:"created_at"`
// DefaultBranch string `json:"default_branch"`
// Description string `json:"description"`
// DownloadsURL string `json:"downloads_url"`
Expand Down Expand Up @@ -76,23 +76,23 @@
// 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"`
// 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 time.Time `json:"updated_at"`
// URL string `json:"url"`
// Watchers float64 `json:"watchers"`
// WatchersCount float64 `json:"watchers_count"`
// }
package gojson

Expand All @@ -104,6 +104,7 @@ import (
"io"
"math"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -491,6 +492,11 @@ func typeForValue(value interface{}, structName string, tags []string, subStruct
v := reflect.TypeOf(value).Name()
if v == "float64" && convertFloats {
v = disambiguateFloatInt(value)
} else if v == "string" {
matched, err := regexp.MatchString(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?((\+|\-)\d{2}:\d{2}|Z)`, value.(string))
if err == nil && matched {
v = "time.Time"
}
}
return v
}
Expand Down
36 changes: 36 additions & 0 deletions json-to-struct_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gojson

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -159,3 +160,38 @@ func TestFmtFieldName(t *testing.T) {
}
}
}

func TestTime(t *testing.T) {
type testCase struct {
input string
expected string
}

expectedTime := "package foopkg\n\ntype FooTime struct {\n\tCreatedTime time.Time `json:\"createdTime\"`\n}\n"
expectedString := "package foopkg\n\ntype FooTime struct {\n\tCreatedTime string `json:\"createdTime\"`\n}\n"

testCases := []testCase{
{"2021-07-28T12:34:56Z", expectedTime},
{"2021-07-28T12:34:56", expectedString},
{"2021-07-28T12:34:56.789Z", expectedTime},
{"2021-07-28T12:34:56.789", expectedString},
{"2021-07-28T12:34:56+06:00", expectedTime},
{"2021-07-28T12:34:56-06:00", expectedTime},
{"2021-07-28T12:34:56-0600", expectedString},
{"2021-07-28T12:34:56.789-06:00", expectedTime},
}
for _, test := range testCases {
t.Run(fmt.Sprintf("when time is %s", test.input), func(t *testing.T) {
jsonIn := fmt.Sprintf(`{"createdTime": "%s"}`, test.input)
bytes, err := Generate(strings.NewReader(jsonIn), ParseJson, "FooTime", "foopkg", []string{"json"}, false, true)
if err != nil {
t.Fatal(err)
}

actual := string(bytes)
if test.expected != actual {
t.Fatalf("'%s' (expected) != '%s' (actual)", test.expected, actual)
}
})
}
}