@@ -62,6 +62,8 @@ func NewComponent(uniqueId string) ComponentBase {
62
62
}
63
63
}
64
64
65
+ // GetProperty returns the first match for the particular property you're after. Please consider using:
66
+ // ComponentProperty.Required to determine if GetProperty or GetProperties is more appropriate.
65
67
func (cb * ComponentBase ) GetProperty (componentProperty ComponentProperty ) * IANAProperty {
66
68
for i := range cb .Properties {
67
69
if cb .Properties [i ].IANAToken == string (componentProperty ) {
@@ -71,6 +73,31 @@ func (cb *ComponentBase) GetProperty(componentProperty ComponentProperty) *IANAP
71
73
return nil
72
74
}
73
75
76
+ // GetProperties returns all matches for the particular property you're after. Please consider using:
77
+ // ComponentProperty.Singular/ComponentProperty.Multiple to determine if GetProperty or GetProperties is more appropriate.
78
+ func (cb * ComponentBase ) GetProperties (componentProperty ComponentProperty ) []* IANAProperty {
79
+ var result []* IANAProperty
80
+ for i := range cb .Properties {
81
+ if cb .Properties [i ].IANAToken == string (componentProperty ) {
82
+ result = append (result , & cb .Properties [i ])
83
+ }
84
+ }
85
+ return result
86
+ }
87
+
88
+ // HasProperty returns true if a component property is in the component.
89
+ func (cb * ComponentBase ) HasProperty (componentProperty ComponentProperty ) bool {
90
+ for i := range cb .Properties {
91
+ if cb .Properties [i ].IANAToken == string (componentProperty ) {
92
+ return true
93
+ }
94
+ }
95
+ return false
96
+ }
97
+
98
+ // SetProperty replaces the first match for the particular property you're setting, otherwise adds it. Please consider using:
99
+ // ComponentProperty.Singular/ComponentProperty.Multiple to determine if AddProperty, SetProperty or ReplaceProperty is
100
+ // more appropriate.
74
101
func (cb * ComponentBase ) SetProperty (property ComponentProperty , value string , params ... PropertyParameter ) {
75
102
for i := range cb .Properties {
76
103
if cb .Properties [i ].IANAToken == string (property ) {
@@ -86,6 +113,17 @@ func (cb *ComponentBase) SetProperty(property ComponentProperty, value string, p
86
113
cb .AddProperty (property , value , params ... )
87
114
}
88
115
116
+ // ReplaceProperty replaces all matches of the particular property you're setting, otherwise adds it. Returns a slice
117
+ // of removed properties. Please consider using:
118
+ // ComponentProperty.Singular/ComponentProperty.Multiple to determine if AddProperty, SetProperty or ReplaceProperty is
119
+ // more appropriate.
120
+ func (cb * ComponentBase ) ReplaceProperty (property ComponentProperty , value string , params ... PropertyParameter ) []IANAProperty {
121
+ removed := cb .RemoveProperty (property )
122
+ cb .AddProperty (property , value , params ... )
123
+ return removed
124
+ }
125
+
126
+ // AddProperty appends a property
89
127
func (cb * ComponentBase ) AddProperty (property ComponentProperty , value string , params ... PropertyParameter ) {
90
128
r := IANAProperty {
91
129
BaseProperty {
@@ -101,16 +139,44 @@ func (cb *ComponentBase) AddProperty(property ComponentProperty, value string, p
101
139
cb .Properties = append (cb .Properties , r )
102
140
}
103
141
104
- // RemoveProperty removes from the component all properties that has
105
- // the name passed in removeProp.
106
- func (cb * ComponentBase ) RemoveProperty (removeProp ComponentProperty ) {
142
+ // RemoveProperty removes from the component all properties that is of a particular property type, returning an slice of
143
+ // removed entities
144
+ func (cb * ComponentBase ) RemoveProperty (removeProp ComponentProperty ) [] IANAProperty {
107
145
var keptProperties []IANAProperty
146
+ var removedProperties []IANAProperty
108
147
for i := range cb .Properties {
109
148
if cb .Properties [i ].IANAToken != string (removeProp ) {
110
149
keptProperties = append (keptProperties , cb .Properties [i ])
150
+ } else {
151
+ removedProperties = append (removedProperties , cb .Properties [i ])
152
+ }
153
+ }
154
+ cb .Properties = keptProperties
155
+ return removedProperties
156
+ }
157
+
158
+ // RemovePropertyByValue removes from the component all properties that has a particular property type and value,
159
+ // return a count of removed properties
160
+ func (cb * ComponentBase ) RemovePropertyByValue (removeProp ComponentProperty , value string ) []IANAProperty {
161
+ return cb .RemovePropertyByFunc (removeProp , func (p IANAProperty ) bool {
162
+ return p .Value == value
163
+ })
164
+ }
165
+
166
+ // RemovePropertyByFunc removes from the component all properties that has a particular property type and the function
167
+ // remove returns true for
168
+ func (cb * ComponentBase ) RemovePropertyByFunc (removeProp ComponentProperty , remove func (p IANAProperty ) bool ) []IANAProperty {
169
+ var keptProperties []IANAProperty
170
+ var removedProperties []IANAProperty
171
+ for i := range cb .Properties {
172
+ if cb .Properties [i ].IANAToken != string (removeProp ) && remove (cb .Properties [i ]) {
173
+ keptProperties = append (keptProperties , cb .Properties [i ])
174
+ } else {
175
+ removedProperties = append (removedProperties , cb .Properties [i ])
111
176
}
112
177
}
113
178
cb .Properties = keptProperties
179
+ return removedProperties
114
180
}
115
181
116
182
const (
0 commit comments