Skip to content

Commit

Permalink
finish function refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mfleader committed Feb 20, 2024
1 parent 690ed39 commit ac7fd1c
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions schema/oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,16 @@ func (o OneOfSchema[KeyType]) UnserializeType(data any) (result any, err error)
}
}

cloneData := maps.Clone(typedData)
if !o.DiscriminatorInlined {
delete(cloneData, o.DiscriminatorFieldNameValue)
}
cloneData := o.deleteDiscriminator(typedData)
unserializedData, err := selectedType.Unserialize(cloneData)
if err != nil {
return result, err
}

unserializedMap, ok := unserializedData.(map[string]any)
if ok {
unserializedMap[o.DiscriminatorFieldNameValue] = discriminator
return unserializedMap, nil
}

return saveConvertTo(unserializedData, o.ReflectedType())
}

Expand All @@ -146,11 +141,7 @@ func (o OneOfSchema[KeyType]) ValidateType(data any) error {
}
dataMap, ok := data.(map[string]any)
if ok {
cloneData := maps.Clone(dataMap)
if !o.DiscriminatorInlined {
delete(cloneData, o.DiscriminatorFieldNameValue)
}
data = cloneData
data = o.deleteDiscriminator(dataMap)
}
if err := underlyingType.Validate(data); err != nil {
return ConstraintErrorAddPathSegment(err, fmt.Sprintf("{oneof[%v]}", discriminatorValue))
Expand All @@ -165,11 +156,7 @@ func (o OneOfSchema[KeyType]) SerializeType(data any) (any, error) {
}
dataMap, ok := data.(map[string]any)
if ok {
cloneData := maps.Clone(dataMap)
if !o.DiscriminatorInlined {
delete(cloneData, o.DiscriminatorFieldNameValue)
}
data = cloneData
data = o.deleteDiscriminator(dataMap)
}
serializedData, err := underlyingType.Serialize(data)
if err != nil {
Expand Down Expand Up @@ -280,11 +267,7 @@ func (o OneOfSchema[KeyType]) validateMap(data map[string]any) (KeyType, Object,
selectedTypeIDAsserted, o.getTypeValues()),
}
}
cloneData := maps.Clone(data)
if !o.DiscriminatorInlined { // Check to see if the discriminator is part of the sub-object.
delete(cloneData, o.DiscriminatorFieldNameValue) // The discriminator isn't part of the object.
}

cloneData := o.deleteDiscriminator(data)
err := selectedSchema.ValidateCompatibility(cloneData)
if err != nil {
return nilKey, nil, &ConstraintError{
Expand Down Expand Up @@ -435,3 +418,13 @@ func (o OneOfSchema[KeyType]) validateSubtypeDiscriminatorInlineFields() error {
}
return nil
}

func (o OneOfSchema[KeyType]) deleteDiscriminator(mymap map[string]any) map[string]any {
// the discriminator is not a property of the subtype
if !o.DiscriminatorInlined {
cloneData := maps.Clone(mymap)
delete(cloneData, o.DiscriminatorFieldNameValue)
return cloneData
}
return mymap
}

0 comments on commit ac7fd1c

Please sign in to comment.