Adds support for multi-types invopop/jsonschema#134#140
Open
PopescuStefanRadu wants to merge 2 commits intoinvopop:mainfrom
Open
Adds support for multi-types invopop/jsonschema#134#140PopescuStefanRadu wants to merge 2 commits intoinvopop:mainfrom
PopescuStefanRadu wants to merge 2 commits intoinvopop:mainfrom
Conversation
samlown
requested changes
Oct 7, 2024
Contributor
samlown
left a comment
There was a problem hiding this comment.
Solid start! Thanks! I'd suggest refactoring the method naming and also cover more of the edge cases with tests.
Comment on lines
+97
to
+99
| type Type struct { | ||
| Types []string | ||
| } |
Contributor
There was a problem hiding this comment.
Did you already consider this?
Suggested change
| 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.)
| } | ||
|
|
||
| // MarshalJSON implements json.Marshaler | ||
| func (tp *Type) MarshalJSON() ([]byte, error) { |
Contributor
There was a problem hiding this comment.
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 |
Contributor
There was a problem hiding this comment.
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) |
Contributor
There was a problem hiding this comment.
I couldn't see any tests for this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.