idubinskiy / schematyper Goto Github PK
View Code? Open in Web Editor NEWGenerates Go types based on JSON Schemas
License: MIT License
Generates Go types based on JSON Schemas
License: MIT License
Schematyper sometimes embeds structs and sometimes does not.
Repro:
Run ./generate
from ignition several times. Note the output is not deterministic.
This might have to do with the parent struct and the child struct having the same name?
input example:
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"name$": {
"type": "string"
},
"name-": {
"type": "string"
},
"schema": {
"type": "string"
},
"$schema": {
"type": "string"
}
}
}
the output result:
type Test struct {
Name string `json:"name,omitempty"`
Name string `json:"name$,omitempty"`
Name string `json:"name-,omitempty"`
Schema string `json:"$schema,omitempty"`
Schema string `json:"schema,omitempty"`
}
panics with the following message
2020/03/21 13:07:39 Can't resolve: (#/items/properties/collection-editor/items #/items/properties/reviewed-author/items #/items/properties/original-author/items #/items/properties/director/items #/items/properties/interviewer/items # #/items/properties/container-author/items #/items/properties/editor/items #/items/properties/composer/items #/items)
when running against this file https://raw.githubusercontent.com/citation-style-language/schema/master/csl-data.json
Right now using allOf
results in embedding any $ref
s and creating new structs for those that are defined in a new block. For those not using $ref
would it be possible (and correct?) to generate those as members of the struct that currently embeds the generated one? I.e. instead of generating:
type Bar struct { ... }
type FooEmbedded1 struct {
A int
B string
}
type Foo struct {
Bar
FooEmbedded1
}
for an allOf block including
$ref: "path/to/barand an object block with
Aand
B` generate:
type Bar struct { ... }
type Foo struct {
Bar
A int
B string
}
I cannot convert the schema like follows to go code.
I'm using prmd and combine some schemas. It generates nested schema like this.
So I'd like you to support the nested schema.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"app": {
"definitions": {
"domains": {
"items": {
"$ref": "#/definitions/domain"
},
"type": "array"
},
"name": {
"pattern": "^[a-z][a-z0-9-]{2,30}$",
"type": "string"
}
},
"properties": {
"domains": {
"$ref": "#/definitions/app/definitions/domains"
},
"name": {
"$ref": "#/definitions/app/definitions/name"
}
},
"required": [
"name"
],
"type": "object"
},
"domain": {
"definitions": {
"name": {
"format": "hostname",
"type": "string"
}
},
"properties": {
"name": {
"$ref": "#/definitions/domain/definitions/name"
}
},
"required": [
"name"
],
"type": "object"
}
},
"properties": {
"app": {
"$ref": "#/definitions/app"
},
"domain": {
"$ref": "#/definitions/domain"
}
},
"type": "object"
}
input
{ "description": "JSON schema for CSL input data", "$schema": "http://json-schema.org/draft-03/schema#", "id": "https://github.com/citation-style-language/schema/raw/master/csl-data.json", "type": "object", "properties": { "family": { "type": "string" }, "given": { "type": "string" }, "dropping-particle": { "type": "string" }, "non-dropping-particle": { "type": "string" }, "suffix": { "type": "string" }, "comma-suffix": { "type": [ "string", "number", "boolean" ] }, "static-ordering": { "type": [ "string", "number", "boolean" ] }, "literal": { "type": "string" }, "parse-names": { "type": [ "string", "number", "boolean" ] } }, "additionalProperties": false }
output
`package main
// generated by "schematyper csl-data.json" -- DO NOT EDIT
// JSON schema for CSL input data
type cslData struct {
CommaSuffix json:"comma-suffix,omitempty"
DroppingParticle string json:"dropping-particle,omitempty"
Family string json:"family,omitempty"
Given string json:"given,omitempty"
Literal string json:"literal,omitempty"
NonDroppingParticle string json:"non-dropping-particle,omitempty"
ParseNames json:"parse-names,omitempty"
StaticOrdering json:"static-ordering,omitempty"
Suffix string json:"suffix,omitempty"
}
`
Two objects with the same name will generate two types with the same name, which causes a compiler error.
It would be good to support allowing specifying struct tags in some way. JSON schema doesn't have a concept which maps nicely to it, but maybe something like additionalProperties
or comments
could be hijacked for use. For Ignition I'm trying to find a way to note additional metadata about each key (specifically which one acts as a unique identifier for the struct) and struct tags would be ideal.
I use an allOf
in my schema to avoid a little duplication,
"file": {
"allOf": [
{
"$ref": "#/definitions/storage/definitions/node"
},
{
"properties": {
"contents": {
"$ref": "#/definitions/storage/definitions/file-contents"
}
}
}
]
},
This results in the File
type in the generated code being this:
type File interface{}
If this is challenging to fix I could always duplicate the node
properties in my schema.
Link to my schema again: http://lpaste.net/6280590999756472320
derek@proton 2 ~/go/src/github.com/coreos/ignition/schema> schematyper ignition.json
2017/02/03 11:25:45 Can't resolve: (#)
I get this error when I try to give schematyper my schema. Any clue how to fix this?
This is ignition.json
, if it helps: http://lpaste.net/5721431062916104192
Need to sort fields and types to avoid moving things around too much (and creating diff nose) because Go doesn't guarantee the order of iteration for maps.
Using the Ignition schema and running:
schematyper --package=types schema/ignition.json -o config/types/schema.go --root-type=Config
from the root of the Ignition source will sometimes generate schemas with Passwd.PasswdUser.Groups
as of type []Group
(where Group
is defined as type Group string
) and sometimes as type []PasswdUserGroup
(where PasswdUserGroup
is also defined as type PasswdUserGroup string
)
These names should not change between runs.
To Reproduce:
schematyper
is in your path./generate && git diff
several times and note the schema it generates differs between runsI suspect it has something to do with the fact that there are multiple things named "group".
Workaround:
Run schematyper
until the ouptut matches what you expect.
based on the following JSONSchema I would like to propose the generation of a New() function.
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"type",
"data"
],
"properties": {
"type": {
"$id": "#/properties/type",
"type": "string",
"title": "Type",
"default": "notify_event",
"examples": [
"notify_event"
],
"pattern": "^(.*)$"
},
"data": {
"$id": "#/properties/data",
"type": "object",
"title": "Data",
"required": [
"resourceId",
"type",
"title",
"content"
],
"properties": {
"resourceId": {
"$id": "#/properties/data/properties/resourceId",
"type": "string",
"title": "Resourceid",
"default": "",
"examples": [
"1a2s3d4f"
],
"pattern": "^(.*)$"
},
"type": {
"$id": "#/properties/data/properties/type",
"type": "string",
"title": "Type",
"default": "rule",
"examples": [
"rule"
],
"pattern": "^(.*)$"
},
"title": {
"$id": "#/properties/data/properties/title",
"type": "string",
"title": "Title",
"default": "",
"examples": [
"rule violation"
],
"pattern": "^(.*)$"
},
"content": {
"$id": "#/properties/data/properties/content",
"type": "string",
"title": "Content",
"default": "",
"examples": [
"some explaination about rule violation"
],
"pattern": "^(.*)$"
}
}
}
}
}
Proposed Output:
package main
// generated by "schematyper schema.json" -- DO NOT EDIT
const (
defaultType string = "notify_event"
defaultDataType string = "rule"
)
type schema struct {
Data theData `json:"data"`
Type string `json:"type"`
}
type data struct {
Content string `json:"content"`
Resourceid string `json:"resourceId"`
Title string `json:"title"`
Type string `json:"type"`
}
func NewSchema() *schema {
return &schema{
Data: data{
Content: "",
Resourceid: "",
Title: "",
Type: defaultDataType,
},
Type: defaultType,
}
}
func NewData() *data {
return &data{
Content: "",
Resourceid: "",
Title: "",
Type: defaultDataType,
}
}
I am parsing a schema document based on JSON Schema version 7 and have run into the following error.
[yamada@localhost device-sender]$ schematyper data.json
2018/12/21 17:08:21 Can't disabiguate: (#/properties/d/items #)
I have verified the schema format should be correct, as it was largely generated in this format according to https://www.jsonschema.net, the only thing taht was added are title names, and pattern matching.
I would expect that the standard array type below which adheres to JSON schema v7 should be recognized with no ambiguities.
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/system/interface/data.json",
"type": "object",
"title": "Stamp Message Schema",
"required": [
"t",
"d"
],
"properties": {
"t": {
"$id": "#/properties/t",
"type": "string",
"title": "msgType",
"default": "",
"examples": [
"B"
],
"pattern": "^([B])$"
},
"d": {
"$id": "#/properties/d",
"type": "array",
"title": "dataArray",
"items": {
"$id": "#/properties/d/items",
"type": "object",
"title": "data",
"required": [
"64",
"dt",
"id"
],
"properties": {
"64": {
"$id": "#/properties/d/items/properties/64",
"type": "string",
"title": "base64",
"default": "",
"examples": [
"aGVsbG8gd29ybGQ="
],
"pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
},
"dt": {
"$id": "#/properties/d/items/properties/dt",
"type": "string",
"title": "dataType",
"default": "",
"examples": [
"C"
],
"pattern": "^(.*)$"
},
"id": {
"$id": "#/properties/d/items/properties/id",
"type": "integer",
"title": "ID",
"default": "",
"examples": [
1
]
}
}
}
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.