-
Notifications
You must be signed in to change notification settings - Fork 94
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
Adds support for multi-types invopop/jsonschema#134 #140
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid start! Thanks! I'd suggest refactoring the method naming and also cover more of the edge cases with tests.
type Type struct { | ||
Types []string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you already consider this?
type Type struct { | |
Types []string | |
} | |
type Type []string |
I'd also suggest adding helper methods to avoid all the messy assignments, instead of:
&Type{Types: []string{"string"}}
I'd expect:
MakeType("string")
With a method pattern that looks like:
func MakeType(typ ...string) Type
(I'm using Make
in the method name as opposed to New
reflecting the underlying slice instead of an object.)
@@ -1112,6 +1125,33 @@ func (t *Schema) MarshalJSON() ([]byte, error) { | |||
return append(b, m[1:]...), nil | |||
} | |||
|
|||
// MarshalJSON implements json.Marshaler | |||
func (tp *Type) MarshalJSON() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd define these methods alongside the Type
struct in the schema.go
file.
func (tp *Type) MarshalJSON() ([]byte, error) { | ||
switch len(tp.Types) { | ||
case 0: | ||
return []byte("[]"), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct? It should be tested either way.
var v string | ||
err2 := json.Unmarshal(data, &v) | ||
if err2 != nil { | ||
return fmt.Errorf("could not read type into slice: %v, nor into string: %w", err.Error(), err2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't see any tests for this.
No description provided.