Skip to content

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

Open
PopescuStefanRadu wants to merge 2 commits intoinvopop:mainfrom
PopescuStefanRadu:main
Open

Adds support for multi-types invopop/jsonschema#134#140
PopescuStefanRadu wants to merge 2 commits intoinvopop:mainfrom
PopescuStefanRadu:main

Conversation

@PopescuStefanRadu
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
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 thread schema.go
Comment on lines +97 to +99
type Type struct {
Types []string
}
Copy link
Copy Markdown
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.)

Comment thread reflect.go
}

// MarshalJSON implements json.Marshaler
func (tp *Type) MarshalJSON() ([]byte, error) {
Copy link
Copy Markdown
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.

Comment thread reflect.go
func (tp *Type) MarshalJSON() ([]byte, error) {
switch len(tp.Types) {
case 0:
return []byte("[]"), nil
Copy link
Copy Markdown
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.

Comment thread reflect.go
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
Copy Markdown
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