Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a type-switch #102

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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