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

Why not gengerate int enum const #46

Open
fangxlmr opened this issue May 5, 2022 · 0 comments
Open

Why not gengerate int enum const #46

fangxlmr opened this issue May 5, 2022 · 0 comments

Comments

@fangxlmr
Copy link

fangxlmr commented May 5, 2022

Say we have this schema

{
    "title": "Example JSON schema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "required": [
        "values"
    ],
    "properties": {
        "values": {
            "description": "example enum values",
            "enum": [
                1,
                2,
                3
            ]
        }
    }
}

The following generated codes only contains type with no int const, say, const SchemaValuesA1 SchemaValues = 1,

Generated code
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package generated

import "fmt"
import "reflect"
import "encoding/json"

type SchemaValues int

var enumValues_SchemaValues = []interface{}{
        1,
        2,
        3,
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *SchemaValues) UnmarshalJSON(b []byte) error {
        var v int
        if err := json.Unmarshal(b, &v); err != nil {
                return err
        }
        var ok bool
        for _, expected := range enumValues_SchemaValues {
                if reflect.DeepEqual(v, expected) {
                        ok = true
                        break
                }
        }
        if !ok {
                return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SchemaValues, v)
        }
        *j = SchemaValues(v)
        return nil
}

type Schema struct {
        // example enum values
        Values SchemaValues `json:"values"`
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *Schema) UnmarshalJSON(b []byte) error {
        var raw map[string]interface{}
        if err := json.Unmarshal(b, &raw); err != nil {
                return err
        }
        if v, ok := raw["values"]; !ok || v == nil {
                return fmt.Errorf("field values: required")
        }
        type Plain Schema
        var plain Plain
        if err := json.Unmarshal(b, &plain); err != nil {
                return err
        }
        *j = Schema(plain)
        return nil
}

If I replace int values by string values, like this

{
    "title": "Example JSON schema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "required": [
        "values"
    ],
    "properties": {
        "values": {
            "description": "example enum values",
            "type": "integer",
            "enum": [
                "1",      <<--- replace with string enum values
                "2",
                "3"
            ]
        }
    }
}

then I got generated code with consts, like const SchemaValuesA1 SchemaValues = "1" which can be accessed outside the package so that it's more convinient than convert int to SchemaValues type.

Generated code
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package generated

import "fmt"
import "reflect"
import "encoding/json"

type Schema struct {
        // example enum values
        Values SchemaValues `json:"values"`
}

var enumValues_SchemaValues = []interface{}{
        "1",
        "2",
        "3",
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *SchemaValues) UnmarshalJSON(b []byte) error {
        var v string
        if err := json.Unmarshal(b, &v); err != nil {
                return err
        }
        var ok bool
        for _, expected := range enumValues_SchemaValues {
                if reflect.DeepEqual(v, expected) {
                        ok = true
                        break
                }
        }
        if !ok {
                return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SchemaValues, v)
        }
        *j = SchemaValues(v)
        return nil
}

type SchemaValues string

const SchemaValuesA1 SchemaValues = "1"      <<---- THIS IS WAHT I WANT (exported consts)
const SchemaValuesA2 SchemaValues = "2"
const SchemaValuesA3 SchemaValues = "3"

// UnmarshalJSON implements json.Unmarshaler.
func (j *Schema) UnmarshalJSON(b []byte) error {
        var raw map[string]interface{}
        if err := json.Unmarshal(b, &raw); err != nil {
                return err
        }
        if v, ok := raw["values"]; !ok || v == nil {
                return fmt.Errorf("field values: required")
        }
        type Plain Schema
        var plain Plain
        if err := json.Unmarshal(b, &plain); err != nil {
                return err
        }
        *j = Schema(plain)
        return nil
}
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