forked from ChimeraCoder/gojson
/
gojson.go
139 lines (126 loc) · 4.27 KB
/
gojson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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 (
"flag"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"strings"
. "github.com/ChimeraCoder/gojson"
)
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")
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)")
fmtType = flag.String("fmt", "json", "the format of the input data (json or yaml, defaults to json)")
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)")
)
func main() {
flag.Parse()
if *fmtType != "json" && *fmtType != "yaml" {
flag.Usage()
fmt.Fprintln(os.Stderr, "fmt must be json or yaml")
os.Exit(1)
}
tagList := make([]string, 0)
if tags == nil || *tags == "" || *tags == "fmt" {
tagList = append(tagList, *fmtType)
} else {
tagList = strings.Split(*tags, ",")
}
if isInteractive() && *inputName == "" {
flag.Usage()
fmt.Fprintln(os.Stderr, "Expects input on stdin")
os.Exit(1)
}
var input io.Reader
input = os.Stdin
if *inputName != "" {
f, err := os.Open(*inputName)
if err != nil {
log.Fatalf("reading input file: %s", err)
}
defer f.Close()
input = f
}
var parser Parser
switch *fmtType {
case "json":
parser = ParseJson
case "yaml":
parser = ParseYaml
}
if output, err := Generate(input, parser, *name, *pkg, tagList, *subStruct); err != nil {
fmt.Fprintln(os.Stderr, "error parsing", err)
os.Exit(1)
} else {
if *outputName != "" {
for i, out := range output {
formatted, err := format.Source([]byte(out))
if err != nil {
log.Fatalf("error formatting: %s", err)
os.Exit(1)
}
if err = ioutil.WriteFile(fmt.Sprintf("%02d_%s", i, *outputName), formatted, 0644); err != nil {
log.Fatalf("writing output: %s", err)
}
}
} else {
fmt.Print(strings.Join(output, "\n\n"))
}
}
}
// Return true if os.Stdin appears to be interactive
func isInteractive() bool {
fileInfo, err := os.Stdin.Stat()
if err != nil {
return false
}
return fileInfo.Mode()&(os.ModeCharDevice|os.ModeCharDevice) != 0
}