Skip to content

Commit

Permalink
use vary to express anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 2, 2023
1 parent f453230 commit 23946e7
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 176 deletions.
7 changes: 4 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/NaturalSelectionLabs/jschema"
"github.com/NaturalSelectionLabs/jschema/lib/test"
"github.com/ysmood/vary"
)

func ExampleNew() {
Expand Down Expand Up @@ -70,15 +71,15 @@ func ExampleSchemas() {
type Metadata interface{}

// Make the metadata field accept either A or B
IMetadata := jschema.DefineI(schemas, new(Metadata))
iMetadata := vary.New(new(Metadata))

type A string

IMetadata.Define(A(""))
iMetadata.Add(A(""))

type B int

IMetadata.Define(B(0))
iMetadata.Add(B(0))

type Node struct {
Name int `json:"name"`
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.20

require (
github.com/xeipuuv/gojsonschema v1.2.0
github.com/ysmood/got v0.34.2
github.com/ysmood/got v0.35.0
github.com/ysmood/vary v0.2.0
)

require (
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/ysmood/gop v0.0.2 // indirect
github.com/ysmood/gop v0.1.0 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/ysmood/gop v0.0.2 h1:VuWweTmXK+zedLqYufJdh3PlxDNBOfFHjIZlPT2T5nw=
github.com/ysmood/gop v0.0.2/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.34.2 h1:oN4DDWwpN5BdNPqYtOIW8a13CsAdzTOECTND7pQQ148=
github.com/ysmood/got v0.34.2/go.mod h1:yddyjq/PmAf08RMLSwDjPyCvHvYed+WjHnQxpH851LM=
github.com/ysmood/gop v0.1.0 h1:BkW6hF0j/hZNyZv+FOPU7op1zuDqRa7x7GJyygb5uf4=
github.com/ysmood/gop v0.1.0/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.35.0 h1:AA9QDCcSuueB9H8OexNHj+VKU1iMDekyS9hWFAApwho=
github.com/ysmood/got v0.35.0/go.mod h1:R7naGpP/qR1i9b5xOCkQnfuEAUPq0TROuTffdxbJoIc=
github.com/ysmood/vary v0.2.0 h1:k4WClkNuee8Xd/cXk+tERoIVZThLCLF0dECxbhgM2Fw=
github.com/ysmood/vary v0.2.0/go.mod h1:X9WYOJwS8Hyeh7Qs08haaeelNru/UIOPUggUnRwxHmM=
25 changes: 0 additions & 25 deletions interface.go

This file was deleted.

134 changes: 0 additions & 134 deletions interface_test.go

This file was deleted.

43 changes: 35 additions & 8 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"encoding/json"
"fmt"
"reflect"

"github.com/ysmood/vary"
)

type Schemas struct {
refPrefix string
types Types
handlers map[Ref]Handler
names map[string]map[string]int
refPrefix string
types Types
handlers map[Ref]Handler
names map[string]map[string]int
interfaces vary.Interfaces
}

type Types map[string]*Schema
Expand All @@ -25,13 +28,20 @@ func New(refPrefix string) Schemas {
}

return Schemas{
refPrefix: refPrefix,
types: Types{},
handlers: map[Ref]Handler{},
names: map[string]map[string]int{},
refPrefix: refPrefix,
types: Types{},
handlers: map[Ref]Handler{},
names: map[string]map[string]int{},
interfaces: vary.Default,
}
}

func NewWithInterfaces(refPrefix string, interfaces vary.Interfaces) Schemas {
s := New(refPrefix)
s.interfaces = interfaces
return s
}

// Schema is designed for typescript conversion.
// Its fields is a strict subset of json schema fields.
type Schema struct {
Expand Down Expand Up @@ -112,6 +122,10 @@ func (s Schemas) DefineT(t reflect.Type) *Schema { //nolint: cyclop,gocyclo
return &Schema{Ref: &r}
}

if iter := s.interfaces[vary.NewID(t)]; iter != nil {
return s.defineInstances(iter)
}

//nolint: exhaustive
switch t.Kind() {
case reflect.Interface:
Expand Down Expand Up @@ -238,6 +252,19 @@ func (s Schemas) DefineT(t reflect.Type) *Schema { //nolint: cyclop,gocyclo
return scm
}

func (s Schemas) defineInstances(i *vary.Interface) *Schema {
scm := s.DefineT(i.Self)
is := s.PeakSchema(scm)
is.Type = ""

for _, p := range i.Implementations {
ps := s.DefineT(p)
is.AnyOf = append(is.AnyOf, ps)
}

return scm
}

type EnumValues interface {
Values() []string
}
Expand Down
Loading

0 comments on commit 23946e7

Please sign in to comment.