Skip to content

Commit

Permalink
Use a type-switch
Browse files Browse the repository at this point in the history
  • Loading branch information
webbnh committed Sep 3, 2024
1 parent d9c0359 commit 28af219
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions schema/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,35 +683,26 @@ func (a *AnyTypedObject[T]) IDEnforced() bool {
// If a ScopeSchema is found, it extracts the root object schema.
// Returns the ObjectSchema and true if successful, otherwise nil and false.
func ConvertToObjectSchema(typeOrData any) (Object, bool) {
// Try plain object schema
objectSchemaType, ok := typeOrData.(*ObjectSchema)
if ok {
return objectSchemaType, true
}
// Next, try ref schema
refSchemaType, ok := typeOrData.(*RefSchema)
if ok {
return refSchemaType.GetObject(), true
}
// Next, try scope schema.
scopeSchemaType, ok := typeOrData.(*ScopeSchema)
if ok {
return scopeSchemaType.RootObject(), true
switch i := typeOrData.(type) {
case *ObjectSchema:
return i, true
case *RefSchema:
return i.GetObject(), true
case *ScopeSchema:
return i.RootObject(), true
}
// Try extracting the inlined ObjectSchema for types that have an ObjectSchema, like TypedObjectSchema.
value := reflect.ValueOf(typeOrData)
if reflect.Indirect(value).Kind() == reflect.Struct {
field := reflect.Indirect(value).FieldByName("ObjectSchema")
if field.IsValid() {
fieldAsInterface := field.Interface()
objectType, ok2 := fieldAsInterface.(ObjectSchema)
if ok2 {
objectSchemaType = &objectType
ok = true
if objectType, ok := fieldAsInterface.(ObjectSchema); ok {
return &objectType, true
}
}
}
return objectSchemaType, ok
return nil, false
}

func validateObjectIsStruct[T any]() {
Expand Down

0 comments on commit 28af219

Please sign in to comment.