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

Export Helper Function #84

Merged
merged 6 commits into from
Apr 18, 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
72 changes: 39 additions & 33 deletions schema/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,38 +342,6 @@ func (o *ObjectSchema) validateStruct(data any) error {
return o.validateFieldInterdependencies(rawData)
}

func (o *ObjectSchema) 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.Objects()[scopeSchemaType.Root()], true
}
// Try getting the inlined ObjectSchema for objects, like TypedObjectSchema, that do that.
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
}
}
}
return objectSchemaType, ok
}

func (o *ObjectSchema) validateSchemaCompatibility(schemaType Object) error {
fieldData := map[string]any{}
// Validate IDs. This is important because the IDs should match.
Expand Down Expand Up @@ -412,7 +380,7 @@ func (o *ObjectSchema) validateRawCompatibility(typeOrData any) error {

func (o *ObjectSchema) ValidateCompatibility(typeOrData any) error {
// Check if it's a schema. If it is, verify it. If not, verify it as data.
schemaType, ok := o.convertToObjectSchema(typeOrData)
schemaType, ok := ConvertToObjectSchema(typeOrData)
if ok {
// It's a schema, so see if the schema matches
return o.validateSchemaCompatibility(schemaType)
Expand Down Expand Up @@ -684,6 +652,44 @@ func (a *AnyTypedObject[T]) Any() TypedObject[any] {
return a
}

// ConvertToObjectSchema attempts to extract an ObjectSchema from the input.
//
// If an ObjectSchema is found, it returns it.
// If a RefSchema is found, it extracts the cached object schema the ref is referencing.
// 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.Objects()[scopeSchemaType.Root()], true
}
Comment on lines +662 to +676
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it should be a switch statement, no?

	switch v := typeOrData.(type) {
	case *ObjectSchema:
		return v, true
	case *RefSchema:
		return v.GetObject(), true
	case *ScopeSchema:
		return v.Objects()[scopeSchemaType.Root()], true
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth trying. If it fails, lots of tests for ValidateCompatibility will fail.

// 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
}
}
}
return objectSchemaType, ok
}

func validateObjectIsStruct[T any]() {
var defaultValue T
reflectValue := reflect.TypeOf(defaultValue)
Expand Down