Skip to content

Commit

Permalink
change return type to string list for ease of use
Browse files Browse the repository at this point in the history
  • Loading branch information
zachsdd committed Aug 30, 2023
1 parent 9ea04e3 commit 633ff7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
18 changes: 7 additions & 11 deletions internal/lang/funcs/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package funcs

import (
"encoding/json"
"fmt"
"math/big"
"sort"
Expand All @@ -24,7 +23,7 @@ var CidrCollapseFunc = function.New(&function.Spec{
Type: cty.List(cty.String),
},
},
Type: function.StaticReturnType(cty.String),
Type: function.StaticReturnType(cty.List(cty.String)),
RefineResult: refineNotNull,
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
var cidrsInput []string
Expand Down Expand Up @@ -55,18 +54,15 @@ var CidrCollapseFunc = function.New(&function.Spec{
}
}

var collapsedCidrs []string
retVals := []cty.Value{}
for _, net := range cidrs {
collapsedCidrs = append(collapsedCidrs, net.String())
retVals = append(retVals, cty.StringVal(net.String()))
}
sort.Strings(collapsedCidrs)
sort.Slice(retVals, func(i, j int) bool {
return i > j
})

val, err := json.Marshal(collapsedCidrs)
if err != nil {
return cty.UnknownVal(cty.String), err
}

return cty.StringVal(string(val)), nil
return cty.ListVal(retVals), nil
},
})

Expand Down
13 changes: 9 additions & 4 deletions internal/lang/funcs/cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,22 @@ func TestCidrCollapse(t *testing.T) {
cty.StringVal("192.168.0.56/32"),
cty.StringVal("192.0.0.0/8"),
}),
Want: cty.StringVal("[\"192.0.0.0/8\"]"),
Err: "",
Want: cty.ListVal([]cty.Value{
cty.StringVal("192.0.0.0/8"),
}),
Err: "",
},
{
Cidrs: cty.ListVal([]cty.Value{
cty.StringVal("192.168.0.0/16"),
cty.StringVal("167.123.0.42/32"),
cty.StringVal("167.123.0.0/16"),
}),
Want: cty.StringVal("[\"167.123.0.0/16\",\"192.168.0.0/16\"]"),
Err: "",
Want: cty.ListVal([]cty.Value{
cty.StringVal("167.123.0.0/16"),
cty.StringVal("192.168.0.0/16"),
}),
Err: "",
},
}

Expand Down
6 changes: 3 additions & 3 deletions website/docs/language/functions/cidrcollapse.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
page_title: cidrcollapse - Functions - Configuration Language
description: |-
The cidrcollapse function takes a list of CIDR ranges and collapses them into a
json encoded string of superset ranges
list of strings of superset ranges
---

# `cidrcollapse` Function
Expand All @@ -16,11 +16,11 @@ cidrcollapse(cidrs)

`cidrs` must be given as a list of valid CIDR ranges.

The result is a JSON encoded string value of superset ranges.
The result is a string list value of superset ranges.

## Examples

```
> cidrcollapse(["192.168.0.0/16", "167.123.0.42/32", "167.123.0.0/16"])
"[\"167.123.0.0/16\",\"192.168.0.0/16\"]"
["167.123.0.0/16","192.168.0.0/16"]
```

0 comments on commit 633ff7f

Please sign in to comment.