/
component_ref.go
73 lines (64 loc) · 1.69 KB
/
component_ref.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
package model
import (
"fmt"
)
const (
unknownComponentRefError = "component cannot be resolved: %s"
)
type (
//componentRef represents a reference to a component
componentRef struct {
//ref specifies id of the component
ref string
//mandatory indicates if the reference is mandatory
mandatory bool
//env specifies the environment holding the referenced component
env *Environment
//location indicates where the reference is located into the descriptor
location DescriptorLocation
}
//ComponentReferencer allows to access to a component through its reference
ComponentReferencer interface {
//Component returns the referenced component
Component() (Component, error)
//ComponentName returns the referenced component name
ComponentName() string
}
)
func createComponentRef(env *Environment, location DescriptorLocation, ref string, mandatory bool) componentRef {
return componentRef{
env: env,
location: location,
ref: ref,
mandatory: mandatory,
}
}
func (r *componentRef) customize(with componentRef) error {
if with.ref != "" {
r.ref = with.ref
}
r.mandatory = with.mandatory
if !with.location.empty() {
r.location = with.location
}
return nil
}
func (r componentRef) resolve() (Component, error) {
if val, ok := r.env.ekara.Components[r.ref]; ok {
return val, nil
}
return Component{}, fmt.Errorf(unknownComponentRefError, r.ref)
}
func (r componentRef) validationDetails() refValidationDetails {
result := make(map[string]interface{})
for k, v := range r.env.ekara.Components {
result[k] = v
}
return refValidationDetails{
Id: r.ref,
Type: "component",
Mandatory: r.mandatory,
Location: r.location,
Repo: result,
}
}