Skip to content

Commit 1588fe0

Browse files
committed
pkg/net: added package
Support: - various IP types - FQDNs Also supports: - use of single-param builtins as types (consistent with curried builtins). - fixed bug in interpretation of top - allow unexported functions to be used by builtins Change-Id: I3568f2383510a904e00d4cf941c753efd03679ce Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2664 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 60ae081 commit 1588fe0

File tree

13 files changed

+738
-19
lines changed

13 files changed

+738
-19
lines changed

cue/binop.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func binOp(ctx *context, src source, op op, left, right evaluated) (result evalu
5353
return right
5454
}
5555

56+
left = convertBuiltin(left)
57+
right = convertBuiltin(right)
58+
5659
leftKind := left.kind()
5760
rightKind := right.kind()
5861
kind, invert, msg := matchBinOpKind(op, leftKind, rightKind)
@@ -125,7 +128,7 @@ func binOp(ctx *context, src source, op op, left, right evaluated) (result evalu
125128
v := right.binOp(ctx, src, op, left)
126129
// Return the original failure if both fail, as this will result in
127130
// better error messages.
128-
if !isBottom(v) {
131+
if !isBottom(v) || isCustom(v) {
129132
return v
130133
}
131134
}

cue/builtin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ func (x *builtin) name(ctx *context) string {
195195
return fmt.Sprintf("%s.%s", ctx.labelStr(x.pkg), x.Name)
196196
}
197197

198+
func convertBuiltin(v evaluated) evaluated {
199+
x, ok := v.(*builtin)
200+
if ok && len(x.Params) == 1 && x.Result == boolKind {
201+
return &customValidator{v.base(), []evaluated{}, x}
202+
}
203+
return v
204+
}
205+
198206
func (x *builtin) call(ctx *context, src source, args ...evaluated) (ret value) {
199207
if x.Func == nil {
200208
return ctx.mkErr(x, "builtin %s is not a function", x.name(ctx))

cue/builtin_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,57 @@ func TestBuiltins(t *testing.T) {
185185
}, {
186186
test("encoding/yaml", `yaml.MarshalStream([{a: 1}, {b: 2}])`),
187187
`"a: 1\n---\nb: 2\n"`,
188+
}, {
189+
test("net", `net.FQDN & "foo.bar."`),
190+
`"foo.bar."`,
191+
}, {
192+
test("net", `net.FQDN("foo.bararararararararararararararararararararararararararararararararara")`),
193+
`false`,
194+
}, {
195+
test("net", `net.SplitHostPort("[::%lo0]:80")`),
196+
`["::%lo0","80"]`,
197+
}, {
198+
test("net", `net.JoinHostPort("example.com", "80")`),
199+
`"example.com:80"`,
200+
}, {
201+
test("net", `net.JoinHostPort("2001:db8::1", 80)`),
202+
`"[2001:db8::1]:80"`,
203+
}, {
204+
test("net", `net.JoinHostPort([192,30,4,2], 80)`),
205+
`"192.30.4.2:80"`,
206+
}, {
207+
test("net", `net.JoinHostPort([192,30,4], 80)`),
208+
`_|_(error in call to net.JoinHostPort: invalid host [192,30,4])`,
209+
}, {
210+
test("net", `net.IP("23.23.23.23")`),
211+
`true`,
212+
}, {
213+
test("net", `net.IPv4 & "23.23.23.2333"`),
214+
`_|_(invalid value "23.23.23.2333" (does not satisfy net.IPv4()))`,
215+
}, {
216+
test("net", `net.IP("23.23.23.23")`),
217+
`true`,
218+
}, {
219+
test("net", `net.IP("2001:db8::1")`),
220+
`true`,
221+
}, {
222+
test("net", `net.IPv4("2001:db8::1")`),
223+
`false`,
224+
}, {
225+
test("net", `net.IPv4() & "ff02::1:3"`),
226+
`_|_(invalid value "ff02::1:3" (does not satisfy net.IPv4()))`,
227+
}, {
228+
test("net", `net.LoopbackIP([127, 0, 0, 1])`),
229+
`true`,
230+
}, {
231+
test("net", `net.LoopbackIP("127.0.0.1")`),
232+
`true`,
233+
}, {
234+
test("net", `net.ToIP4("127.0.0.1")`),
235+
`[127,0,0,1]`,
236+
}, {
237+
test("net", `net.ToIP16("127.0.0.1")`),
238+
`[0,0,0,0,0,0,0,0,0,0,255,255,127,0,0,1]`,
188239
}, {
189240
test("strings", `strings.ToCamel("AlphaBeta")`),
190241
`"alphaBeta"`,

0 commit comments

Comments
 (0)