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

Adds support for multi-types invopop/jsonschema#134 #140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PopescuStefanRadu
Copy link

No description provided.

Copy link
Contributor

@samlown samlown left a 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.

Comment on lines +97 to +99
type Type struct {
Types []string
}
Copy link
Contributor

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?

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.)

@@ -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) {
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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.

@samlown samlown added the needs tests Not enough tests for this to be accepted label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Not enough tests for this to be accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants