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

Generate New() function for schema with defaults #17

Open
rustysys-dev opened this issue Dec 19, 2019 · 0 comments
Open

Generate New() function for schema with defaults #17

rustysys-dev opened this issue Dec 19, 2019 · 0 comments

Comments

@rustysys-dev
Copy link

rustysys-dev commented Dec 19, 2019

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,
	}
}
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

No branches or pull requests

1 participant