Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for root-node #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
outputFile = kingpin.Flag("out-file", "filename for output; default is <schema>_schematype.go").Short('o').String()
packageName = kingpin.Flag("package", `package name for generated file; default is "main"`).Default("main").String()
rootTypeName = kingpin.Flag("root-type", `name of root type; default is generated from the filename`).String()
rootNodeName = kingpin.Flag("root-node", `name of root node of json; default is whole file`).String()
typeNamesPrefix = kingpin.Flag("prefix", `prefix for non-root types`).String()
inputFile = kingpin.Arg("input", "file containing a valid JSON schema").Required().ExistingFile()
)
Expand Down Expand Up @@ -690,8 +691,22 @@ func main() {
}

var s metaSchema
if err = json.Unmarshal(file, &s); err != nil {
log.Fatalln("Error parsing JSON:", err)
if *rootNodeName != "" {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line.

var sub map[string]metaSchema
if err = json.Unmarshal(file, &sub); err != nil {
log.Fatalln("Error parsing JSON:", err)
}
node, ok := sub[*rootNodeName]
if ok == false {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be !ok.

log.Fatalln(fmt.Sprintf("Error JSON node '%s' does not exists:", *rootNodeName), err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of combining log.Fatalln and fmt.Sprintf, use log.Fatalf. Don't forget to add \n at the end of the format string.

}
//log.Panicf("Error reading file %v:", node)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out code.

s = node
} else {
if err = json.Unmarshal(file, &s); err != nil {
log.Fatalln("Error parsing JSON:", err)
}
}

schemaName := strings.Split(filepath.Base(*inputFile), ".")[0]
Expand Down