Skip to content

Commit da4b109

Browse files
authored
Merge pull request #233 from apelisse/minor-cleanups
Minor cleanups
2 parents bbe0f82 + 8961154 commit da4b109

File tree

7 files changed

+22
-9
lines changed

7 files changed

+22
-9
lines changed

merge/nested_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var nestedTypeParser = func() Parser {
5555
scalar: string
5656
- name: value
5757
type:
58-
scalar: number
58+
scalar: numeric
5959
- name: listOfLists
6060
list:
6161
elementType:

schema/elements.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Atom struct {
7373
}
7474

7575
// Scalar (AKA "primitive") represents a type which has a single value which is
76-
// either numeric, string, or boolean.
76+
// either numeric, string, or boolean, or untyped for any of them.
7777
//
7878
// TODO: split numeric into float/int? Something even more fine-grained?
7979
type Scalar string
@@ -82,6 +82,7 @@ const (
8282
Numeric = Scalar("numeric")
8383
String = Scalar("string")
8484
Boolean = Scalar("boolean")
85+
Untyped = Scalar("untyped")
8586
)
8687

8788
// ElementRelationship is an enum of the different possible relationships

schema/elements_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ func TestCopyInto(t *testing.T) {
151151
{"existingNamedType", []TypeDef{{Name: existing, Atom: a}}, TypeRef{NamedType: &existing}},
152152
}
153153

154-
for _, tt := range tests {
154+
for i := range tests {
155+
tt := tests[i]
155156
t.Run(tt.testName, func(t *testing.T) {
156157
t.Parallel()
157158
s := Schema{

schema/schemaschema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var SchemaSchemaYAML = `types:
110110
scalar: string
111111
- name: deduceInvalidDiscriminator
112112
type:
113-
scalar: bool
113+
scalar: boolean
114114
- name: fields
115115
type:
116116
list:

typed/merge.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func (w *mergingWalker) doLeaf() {
113113
w.rule(w)
114114
}
115115

116-
func (w *mergingWalker) doScalar(t *schema.Scalar) (errs ValidationErrors) {
117-
errs = append(errs, validateScalar(t, w.lhs, "lhs: ")...)
118-
errs = append(errs, validateScalar(t, w.rhs, "rhs: ")...)
119-
if len(errs) > 0 {
120-
return errs
116+
func (w *mergingWalker) doScalar(t *schema.Scalar) ValidationErrors {
117+
// Make sure at least one side is a valid scalar.
118+
lerrs := validateScalar(t, w.lhs, "lhs: ")
119+
rerrs := validateScalar(t, w.rhs, "rhs: ")
120+
if len(lerrs) > 0 && len(rerrs) > 0 {
121+
return append(lerrs, rerrs...)
121122
}
122123

123124
// All scalars are leaf fields.

typed/typed.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef) (*TypedVal
4545
// conforms to the schema, for cases where that has already been checked or
4646
// where you're going to call a method that validates as a side-effect (like
4747
// ToFieldSet).
48+
//
49+
// Deprecated: This function was initially created because validation
50+
// was expensive. Now that this has been solved, objects should always
51+
// be created as validated, using `AsTyped`.
4852
func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeRef schema.TypeRef) *TypedValue {
4953
tv := &TypedValue{
5054
value: v,

typed/validate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ func validateScalar(t *schema.Scalar, v value.Value, prefix string) (errs Valida
102102
if !v.IsBool() {
103103
return errorf("%vexpected boolean, got %v", prefix, v)
104104
}
105+
case schema.Untyped:
106+
if !v.IsFloat() && !v.IsInt() && !v.IsString() && !v.IsBool() {
107+
return errorf("%vexpected any scalar, got %v", prefix, v)
108+
}
109+
default:
110+
return errorf("%vunexpected scalar type in schema: %v", prefix, *t)
105111
}
106112
return nil
107113
}

0 commit comments

Comments
 (0)