/
relationship.go
91 lines (79 loc) · 2.72 KB
/
relationship.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
83
84
85
86
87
88
89
90
91
package expr
import (
"fmt"
"strings"
)
type (
// Relationship describes a uni-directional relationship between two elements.
Relationship struct {
ID string
Source *Element
Description string
Technology string
InteractionStyle InteractionStyleKind
Tags string
URL string
// DestinationPath is used to compute the destination after all DSL has
// completed execution.
DestinationPath string
// Destination is only guaranteed to be initialized after the DSL has
// been executed. It can be used in validations and finalizers.
Destination *Element
// LinkedRelationshipID is the ID of the relationship pointing to the
// container corresponding to the container instance with this
// relationship.
LinkedRelationshipID string
}
// InteractionStyleKind is the enum for possible interaction styles.
InteractionStyleKind int
)
const (
// InteractionUndefined means no interaction style specified in design.
InteractionUndefined InteractionStyleKind = iota
// InteractionSynchronous describes a synchronous interaction.
InteractionSynchronous
// InteractionAsynchronous describes an asynchronous interaction.
InteractionAsynchronous
)
// RelationshipTags lists the tags that are added to all relationships.
var RelationshipTags = []string{"Relationship", "Asynchronous", "Synchronous"}
// EvalName is the qualified name of the expression.
func (r *Relationship) EvalName() string {
var src, dest = "<unknown source>", "<unknown destination>"
if r.Source != nil {
src = r.Source.Name
}
if r.Destination != nil {
dest = r.Destination.Name
}
return fmt.Sprintf("relationship %q [%s -> %s]", r.Description, src, dest)
}
// Finalize computes the destination and adds the "Relationship" tag.
func (r *Relationship) Finalize() {
// prefix tags
if r.InteractionStyle == InteractionAsynchronous {
r.Tags = mergeTags("Asynchronous", strings.Split(r.Tags, ","))
}
r.Tags = mergeTags(RelationshipTags[0], strings.Split(r.Tags, ","))
}
// PrefixTags adds the given tags to the beginning of the comm
// Dup creates a new relationship with identical description, tags, URL,
// technology and interaction style as r. Dup also creates a new ID for the
// result.
func (r *Relationship) Dup(newSrc, newDest *Element) *Relationship {
dup := &Relationship{
Source: newSrc,
InteractionStyle: r.InteractionStyle,
Tags: r.Tags,
URL: r.URL,
Destination: newDest,
Description: r.Description,
Technology: r.Technology,
}
Identify(dup)
return dup
}
// MergeTags adds the given tags. It skips tags already present in e.Tags.
func (r *Relationship) MergeTags(tags ...string) {
r.Tags = mergeTags(r.Tags, tags)
}