Skip to content

Instantly share code, notes, and snippets.

@smothiki
Created March 20, 2017 19:45
Show Gist options
  • Save smothiki/5eb2bff12a2ff38e4f04d5211134b6c7 to your computer and use it in GitHub Desktop.
Save smothiki/5eb2bff12a2ff38e4f04d5211134b6c7 to your computer and use it in GitHub Desktop.
func ParseConstraints(data []byte) ([]types.Constraint, error) {
var constraints []types.Constraint
var dat map[string][]map[string]string
if err := json.Unmarshal(data, &dat); err != nil {
return nil, err
}
val, ok := dat["constraint"]
if !ok {
return nil, nil
}
for _, mapv := range val {
for k, v := range mapv {
if strings.Contains(v, "limit") {
value, err := strconv.Atoi(strings.Split(v, ":")[1])
if err != nil {
return nil, err
}
constraints = append(constraints, types.Constraint{ConstraintType: "limit", Key: k, Value: value})
}
constraints = append(constraints, types.Constraint{ConstraintType: "value", Key: k, Value: v})
}
}
return constraints, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment