-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from SoulKa/feature/code-polishing
Feature/code polishing
- Loading branch information
Showing
12 changed files
with
228 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,11 @@ | ||
package golymorph | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/SoulKa/golymorph/objectpath" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// Polymorphism is the base struct for all polymorphism mappers. It contains the target path to assign the new type to. | ||
type Polymorphism struct { | ||
// targetPath is the path to the object to assign the new type to | ||
targetPath objectpath.ObjectPath | ||
} | ||
|
||
// UnmarshalJSON unmarshals the given JSON data into the given output object using the given TypeResolver. | ||
func UnmarshalJSON(resolver TypeResolver, data []byte, output any) error { | ||
|
||
// parse JSON | ||
var jsonMap map[string]any | ||
if err := json.Unmarshal(data, &jsonMap); err != nil { | ||
return err | ||
} | ||
|
||
// resolve polymorphism | ||
if err := Decode(resolver, jsonMap, output); err != nil { | ||
return err | ||
} | ||
|
||
// success | ||
return nil | ||
} | ||
|
||
// Decode decodes the given source map into the given output object using the given TypeResolver and mapstructure. | ||
func Decode(resolver TypeResolver, source map[string]any, output any) error { | ||
|
||
// create a new event | ||
if err := resolver.AssignTargetType(&source, output); err != nil { | ||
return err | ||
} | ||
|
||
// use mapstructure to unmarshal the payload into the event | ||
if err := mapstructure.Decode(source, output); err != nil { | ||
return err | ||
} | ||
|
||
// success | ||
return nil | ||
// TargetPath is the path to the object to assign the new type to | ||
TargetPath objectpath.ObjectPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package golymorph | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type polymorphismRuleBuilder struct { | ||
polymorphismBuilderBase | ||
rules []Rule | ||
} | ||
|
||
func (b *polymorphismRuleBuilder) UsingRule(rule Rule) polymorphismBuilderRuleAdder { | ||
b.rules = append(b.rules, rule) | ||
return b | ||
} | ||
|
||
func (b *polymorphismRuleBuilder) Build() (error, TypeResolver) { | ||
if len(b.errors) > 0 { | ||
return errors.Join(b.errors...), nil | ||
} | ||
return nil, &RulePolymorphism{ | ||
Polymorphism{ | ||
TargetPath: b.targetPath}, | ||
b.rules} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package golymorph | ||
|
||
import ( | ||
"errors" | ||
"github.com/SoulKa/golymorph/objectpath" | ||
) | ||
|
||
type polymorphismTypeMapBuilder struct { | ||
polymorphismBuilderBase | ||
typeMap TypeMap | ||
discriminatorPath objectpath.ObjectPath | ||
} | ||
|
||
func (b *polymorphismTypeMapBuilder) WithDiscriminatorAt(discriminatorKey string) polymorphismBuilderFinalizer { | ||
if err, path := objectpath.NewObjectPathFromString(discriminatorKey); err != nil { | ||
b.errors = append(b.errors, err) | ||
} else if err := path.ToAbsolutePath(&b.targetPath); err != nil { | ||
b.errors = append(b.errors, err) | ||
} else { | ||
b.discriminatorPath = *path | ||
} | ||
return b | ||
} | ||
|
||
func (b *polymorphismTypeMapBuilder) Build() (error, TypeResolver) { | ||
if len(b.errors) > 0 { | ||
return errors.Join(b.errors...), nil | ||
} | ||
return nil, &TypeMapPolymorphism{ | ||
Polymorphism: Polymorphism{ | ||
TargetPath: b.targetPath}, | ||
DiscriminatorPath: b.discriminatorPath, | ||
TypeMap: b.typeMap} | ||
} |
Oops, something went wrong.