-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
Co-authored-by: github-merge-queue <[email protected]> Co-authored-by: Julien Robert <[email protected]> (cherry picked from commit 4e81d17) # Conflicts: # schema/type.go
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package schema | ||
|
||
// Type is an interface that all types in the schema implement. | ||
// Currently, these are StateObjectType and EnumType. | ||
type Type interface { | ||
// TypeName returns the type's name. | ||
TypeName() string | ||
|
||
// Validate validates the type. | ||
Validate(TypeSet) error | ||
|
||
// isType is a private method that ensures that only types in this package can be marked as types. | ||
isType() | ||
} | ||
|
||
// ReferenceableType is a marker interface that all types that can be the target of Field.ReferencedType implement. | ||
// Currently, this is only EnumType. | ||
type ReferenceableType interface { | ||
Type | ||
|
||
// isReferenceableType is implemented if this is a reference type. | ||
isReferenceableType() | ||
} | ||
|
||
// TypeSet represents something that has types and allows them to be looked up by name. | ||
// Currently, the only implementation is ModuleSchema. | ||
type TypeSet interface { | ||
// LookupType looks up a type by name. | ||
LookupType(name string) (t Type, found bool) | ||
|
||
// LookupEnumType is a convenience method that looks up an EnumType by name. | ||
LookupEnumType(name string) (t EnumType, found bool) | ||
Check failure on line 32 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
// LookupStateObjectType is a convenience method that looks up an StateObjectType by name. | ||
LookupStateObjectType(name string) (t StateObjectType, found bool) | ||
Check failure on line 35 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
// AllTypes calls the given function for each type in the type set. | ||
// This function is compatible with go 1.23 iterators and can be used like this: | ||
// for t := range types.AllTypes { | ||
// // do something with t | ||
// } | ||
AllTypes(f func(Type) bool) | ||
|
||
// EnumTypes calls the given function for each EnumType in the type set. | ||
// This function is compatible with go 1.23 iterators. | ||
EnumTypes(f func(EnumType) bool) | ||
Check failure on line 46 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
// StateObjectTypes calls the given function for each StateObjectType in the type set. | ||
// This function is compatible with go 1.23 iterators. | ||
StateObjectTypes(f func(objectType StateObjectType) bool) | ||
Check failure on line 50 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
// isTypeSet is a private method that ensures that only types in this package can be marked as type sets. | ||
isTypeSet() | ||
} | ||
|
||
// EmptyTypeSet is a schema that contains no types. | ||
// It can be used in Validate methods when there is no schema needed or available. | ||
func EmptyTypeSet() TypeSet { | ||
return emptyTypeSetInst | ||
} | ||
|
||
var emptyTypeSetInst = emptyTypeSet{} | ||
|
||
type emptyTypeSet struct{} | ||
|
||
func (emptyTypeSet) LookupType(string) (Type, bool) { | ||
return nil, false | ||
} | ||
|
||
func (s emptyTypeSet) LookupEnumType(string) (t EnumType, found bool) { | ||
Check failure on line 70 in schema/type.go GitHub Actions / dependency-review
|
||
return EnumType{}, false | ||
Check failure on line 71 in schema/type.go GitHub Actions / dependency-review
|
||
} | ||
|
||
func (s emptyTypeSet) LookupStateObjectType(string) (t StateObjectType, found bool) { | ||
Check failure on line 74 in schema/type.go GitHub Actions / dependency-review
|
||
return StateObjectType{}, false | ||
Check failure on line 75 in schema/type.go GitHub Actions / dependency-review
|
||
} | ||
|
||
func (emptyTypeSet) AllTypes(func(Type) bool) {} | ||
|
||
func (s emptyTypeSet) EnumTypes(func(EnumType) bool) {} | ||
Check failure on line 80 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
func (s emptyTypeSet) StateObjectTypes(func(objectType StateObjectType) bool) {} | ||
Check failure on line 82 in schema/type.go GitHub Actions / dependency-review
|
||
|
||
func (emptyTypeSet) isTypeSet() {} |