mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-07 23:07:14 +02:00
Add std.xor for 2 booleans
This commit is contained in:
parent
772ebba669
commit
91ebf4b09e
13
builtins.go
13
builtins.go
@ -704,6 +704,18 @@ func builtinNegation(i *interpreter, x value) (value, error) {
|
|||||||
return makeValueBoolean(!b.value), nil
|
return makeValueBoolean(!b.value), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func builtinXnor(i *interpreter, xv, yv value) (value, error) {
|
||||||
|
p, err := i.getBoolean(xv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
q, err := i.getBoolean(yv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return makeValueBoolean(p.value == q.value), nil
|
||||||
|
}
|
||||||
|
|
||||||
func builtinXor(i *interpreter, xv, yv value) (value, error) {
|
func builtinXor(i *interpreter, xv, yv value) (value, error) {
|
||||||
p, err := i.getBoolean(xv)
|
p, err := i.getBoolean(xv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -2198,6 +2210,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
|
|||||||
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
|
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
|
||||||
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
|
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
|
||||||
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
|
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
|
||||||
|
&binaryBuiltin{name: "xnor", function: builtinXnor, params: ast.Identifiers{"x", "y"}},
|
||||||
&binaryBuiltin{name: "xor", function: builtinXor, params: ast.Identifiers{"x", "y"}},
|
&binaryBuiltin{name: "xor", function: builtinXor, params: ast.Identifiers{"x", "y"}},
|
||||||
&binaryBuiltin{name: "lstripChars", function: builtinLstripChars, params: ast.Identifiers{"str", "chars"}},
|
&binaryBuiltin{name: "lstripChars", function: builtinLstripChars, params: ast.Identifiers{"str", "chars"}},
|
||||||
&binaryBuiltin{name: "rstripChars", function: builtinRstripChars, params: ast.Identifiers{"str", "chars"}},
|
&binaryBuiltin{name: "rstripChars", function: builtinRstripChars, params: ast.Identifiers{"str", "chars"}},
|
||||||
|
@ -5,12 +5,27 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
)
|
)
|
||||||
|
func check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Diff produces a pretty diff of two files
|
// Diff produces a pretty diff of two files
|
||||||
func Diff(a, b string) string {
|
func Diff(a, b string) string {
|
||||||
|
f, err := os.Create("/Users/salonijuneja/yourfile11.txt")
|
||||||
|
check(err)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
w := bufio.NewWriter(f)
|
||||||
|
|
||||||
|
_, err = fmt.Fprintf(w, "%v\n", a)
|
||||||
|
check(err)
|
||||||
|
w.Flush()
|
||||||
dmp := diffmatchpatch.New()
|
dmp := diffmatchpatch.New()
|
||||||
diffs := dmp.DiffMain(a, b, false)
|
diffs := dmp.DiffMain(a, b, false)
|
||||||
return dmp.DiffPrettyText(diffs)
|
return dmp.DiffPrettyText(diffs)
|
||||||
|
@ -175,6 +175,7 @@ func prepareStdlib(g *typeGraph) {
|
|||||||
// Boolean
|
// Boolean
|
||||||
|
|
||||||
"xor": g.newSimpleFuncType(boolType, "x", "y"),
|
"xor": g.newSimpleFuncType(boolType, "x", "y"),
|
||||||
|
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldContains := map[string][]placeholderID{}
|
fieldContains := map[string][]placeholderID{}
|
||||||
|
1
testdata/builtinXnor.golden
vendored
Normal file
1
testdata/builtinXnor.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
true
|
1
testdata/builtinXnor.jsonnet
vendored
Normal file
1
testdata/builtinXnor.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.xnor(false, false)
|
0
testdata/builtinXnor.linter.golden
vendored
Normal file
0
testdata/builtinXnor.linter.golden
vendored
Normal file
1
testdata/builtinXnor1.golden
vendored
Normal file
1
testdata/builtinXnor1.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
false
|
1
testdata/builtinXnor1.jsonnet
vendored
Normal file
1
testdata/builtinXnor1.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.xnor(true, false)
|
0
testdata/builtinXnor1.linter.golden
vendored
Normal file
0
testdata/builtinXnor1.linter.golden
vendored
Normal file
10
testdata/builtinXnor2.golden
vendored
Normal file
10
testdata/builtinXnor2.golden
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
RUNTIME ERROR: Unexpected type string, expected boolean
|
||||||
|
-------------------------------------------------
|
||||||
|
testdata/builtinXnor2:1:1-24 $
|
||||||
|
|
||||||
|
std.xnor("true", false)
|
||||||
|
|
||||||
|
-------------------------------------------------
|
||||||
|
During evaluation
|
||||||
|
|
||||||
|
|
1
testdata/builtinXnor2.jsonnet
vendored
Normal file
1
testdata/builtinXnor2.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.xnor("true", false)
|
0
testdata/builtinXnor2.linter.golden
vendored
Normal file
0
testdata/builtinXnor2.linter.golden
vendored
Normal file
Loading…
Reference in New Issue
Block a user