-
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.
using generic rules to apply polymorphism
- Loading branch information
Showing
4 changed files
with
80 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package golymorph | ||
|
||
type PolymorphismBuilder struct { | ||
} |
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,40 @@ | ||
package golymorph | ||
|
||
import ( | ||
"fmt" | ||
"github.com/SoulKa/golymorph/objectpath" | ||
"reflect" | ||
) | ||
|
||
type ComparatorType int | ||
|
||
const ( | ||
ComparatorTypeEquality ComparatorType = iota | ||
) | ||
|
||
// Rule is a rule for a polymorphism mapper. | ||
type Rule struct { | ||
// ValuePath is the path to the value in the source to compare. | ||
ValuePath objectpath.ObjectPath | ||
// ComparatorType is the type of comparison to perform. | ||
ComparatorType ComparatorType | ||
// ComparatorValue is the value to compare against. | ||
ComparatorValue any | ||
// NewType is the type to assign to the target if the rule matches. | ||
NewType reflect.Type | ||
} | ||
|
||
// Matches returns true if the source matches the rule. | ||
func (r *Rule) Matches(source any) (error, bool) { | ||
var sourceValue reflect.Value | ||
if err := objectpath.GetValueAtPath(source, r.ValuePath, &sourceValue); err != nil { | ||
return err, false | ||
} | ||
|
||
switch r.ComparatorType { | ||
case ComparatorTypeEquality: | ||
return nil, sourceValue.Interface() == r.ComparatorValue | ||
default: | ||
return fmt.Errorf("unknown comparator type %d", r.ComparatorType), false | ||
} | ||
} |