-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphism_builder.go
82 lines (68 loc) · 2.97 KB
/
polymorphism_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package golymorph
import (
"github.com/SoulKa/golymorph/objectpath"
"strings"
)
type polymorphismBuilderBase struct {
targetPath objectpath.ObjectPath
errors []error
}
type polymorphismBuilderEmpty interface {
// DefineTypeAt defines the target path of the polymorphism. This is the path where the polymorphism
// will be applied, i.e. where the new type is set. For valid paths see objectpath.NewObjectPathFromString.
DefineTypeAt(targetPath string) polymorphismBuilderStrategySelector
}
type polymorphismBuilderStrategySelector interface {
// UsingRule defines a rule that is used to determine the new type. The rules are applied in the
// order they are defined. The first rule that matches is used to determine the new type.
UsingRule(rule Rule) polymorphismBuilderRuleAdder
// UsingTypeMap defines a type map that is used to determine the new type. The type map is applied
UsingTypeMap(typeMap TypeMap) polymorphismBuilderDiscriminatorKeyDefiner
}
type polymorphismBuilderRuleAdder interface {
// UsingRule defines a rule that is used to determine the new type. The rules are applied in the
// order they are defined. The first rule that matches is used to determine the new type.
UsingRule(rule Rule) polymorphismBuilderRuleAdder
// Build creates a new TypeResolver that can be used to resolve a polymorphic type.
Build() (error, TypeResolver)
}
type polymorphismBuilderDiscriminatorKeyDefiner interface {
// WithDiscriminatorAt defines the path to the discriminator key. The discriminator key is used to
// determine the new type. The value of the discriminator key is used to lookup the new type in the
// type map.
WithDiscriminatorAt(discriminatorKey string) polymorphismBuilderFinalizer
}
type polymorphismBuilderFinalizer interface {
// Build creates a new TypeResolver that can be used to resolve a polymorphic type.
Build() (error, TypeResolver)
}
// NewPolymorphismBuilder creates a new polymorphism builder that is used in a human readable way to create a polymorphism.
// It only allows a valid combination of rules and type maps.
func NewPolymorphismBuilder() polymorphismBuilderEmpty {
return &polymorphismBuilderBase{*objectpath.NewSelfReferencePath(), []error{}}
}
func (b *polymorphismBuilderBase) DefineTypeAt(targetPath string) polymorphismBuilderStrategySelector {
// make target path absolute
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
// parse target path
if err, path := objectpath.NewObjectPathFromString(targetPath); err != nil {
b.errors = append(b.errors, err)
} else {
b.targetPath = *path
}
return b
}
func (b *polymorphismBuilderBase) UsingRule(rule Rule) polymorphismBuilderRuleAdder {
return &polymorphismRuleBuilder{
polymorphismBuilderBase: *b,
rules: []Rule{rule},
}
}
func (b *polymorphismBuilderBase) UsingTypeMap(typeMap TypeMap) polymorphismBuilderDiscriminatorKeyDefiner {
return &polymorphismTypeMapBuilder{
polymorphismBuilderBase: *b,
typeMap: typeMap,
}
}