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

Root types aliases #40

Merged
merged 1 commit into from
Apr 14, 2018
Merged

Root types aliases #40

merged 1 commit into from
Apr 14, 2018

Conversation

antoineco
Copy link
Contributor

@antoineco antoineco commented Mar 27, 2018

When a top-level schema is composed only of a simple type (anything but object), generate a type alias for it instead of ignoring it.

2 use cases come to my mind: adding methods to primitive Go types, and working with constants.

Example 1:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://foo",
  "type": "string",
  "enum": [ "A", "B" ]
}
// Code generated by schema-generate. DO NOT EDIT.

package main

// Root
type Root string

Example 2:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://foo",
  "type": "array",
  "title": "foos",
  "items": {
    "type": "object",
    "title": "foo",
    "properties": {
      "fieldA": {
        "type": "string"
      },
      "fieldB": {
        "type": "boolean"
      }
    }
  }
}
// Code generated by schema-generate. DO NOT EDIT.

package main

// Foos
type Foos []Foo

// Foo 
type Foo struct {
  FieldA string `json:"fieldA,omitempty"`
  FieldB bool `json:"fieldB,omitempty"`
}

➡️ Tests results

if err != nil {
fmt.Fprintln(os.Stderr, "Failure generating structs: ", err)
}

var w io.Writer
var w io.Writer = os.Stdout
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@a-h a-h merged commit 7b543f1 into a-h:master Apr 14, 2018
@antoineco antoineco deleted the feature/simple-types branch April 14, 2018 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants