~wallyworld/juju-core/fast-lxc-everywhere

« back to all changes in this revision

Viewing changes to constraints/constraints.go

  • Committer: Tarmac
  • Author(s): Ian Booth
  • Date: 2014-04-24 12:33:19 UTC
  • mfrom: (2664.1.4 constraints-vocab)
  • Revision ID: tarmac-20140424123319-iifvboa1kjuprj7l
[r=wallyworld] Support constraints vocabs

Add support to constraints validation for
vocabs for each attribute. eg arch and
instance-type can only have well defined
values for each provider. The vocab check
is part of constraints validation.

https://codereview.appspot.com/96730043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
206
206
        return val, val.IsValid()
207
207
}
208
208
 
209
 
// attributesWithValues returns the non-zero attribute tags from the constraint.
210
 
func (v *Value) attributesWithValues() []string {
211
 
        var result []string = []string{}
 
209
// attributesWithValues returns the non-zero attribute tags and their values from the constraint.
 
210
func (v *Value) attributesWithValues() (result map[string]interface{}) {
 
211
        result = make(map[string]interface{})
212
212
        for fieldTag, fieldName := range fieldNames {
213
213
                val := reflect.ValueOf(v).Elem().FieldByName(fieldName)
214
214
                if !val.IsNil() {
215
 
                        result = append(result, fieldTag)
 
215
                        result[fieldTag] = val.Elem().Interface()
216
216
                }
217
217
        }
218
218
        return result
220
220
 
221
221
// hasAny returns any attrTags for which the constraint has a non-nil value.
222
222
func (v *Value) hasAny(attrTags ...string) []string {
223
 
        withValues := v.attributesWithValues()
224
 
        containsFunc := func(values []string, value string) bool {
225
 
                for _, v := range values {
226
 
                        if v == value {
227
 
                                return true
228
 
                        }
229
 
                }
230
 
                return false
231
 
        }
 
223
        attrValues := v.attributesWithValues()
232
224
        var result []string = []string{}
233
225
        for _, tag := range attrTags {
234
 
                if containsFunc(withValues, tag) {
 
226
                _, ok := attrValues[tag]
 
227
                if ok {
235
228
                        result = append(result, tag)
236
229
                }
237
230
        }