mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-07 23:07:14 +02:00
std.md5
This commit is contained in:
parent
999ff267f1
commit
8a80309137
12
builtins.go
12
builtins.go
@ -18,6 +18,8 @@ package jsonnet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
@ -373,6 +375,15 @@ func builtinType(e *evaluator, xp potentialValue) (value, error) {
|
|||||||
return makeValueString(x.typename()), nil
|
return makeValueString(x.typename()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func builtinMd5(e *evaluator, xp potentialValue) (value, error) {
|
||||||
|
x, err := e.evaluateString(xp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hash := md5.Sum([]byte(string(x.value)))
|
||||||
|
return makeValueString(hex.EncodeToString(hash[:])), nil
|
||||||
|
}
|
||||||
|
|
||||||
func makeDoubleCheck(e *evaluator, x float64) (value, error) {
|
func makeDoubleCheck(e *evaluator, x float64) (value, error) {
|
||||||
if math.IsNaN(x) {
|
if math.IsNaN(x) {
|
||||||
return nil, e.Error("Not a number")
|
return nil, e.Error("Not a number")
|
||||||
@ -616,4 +627,5 @@ var funcBuiltins = map[string]evalCallable{
|
|||||||
"exponent": &UnaryBuiltin{name: "exponent", function: builtinExponent, parameters: ast.Identifiers{"x"}},
|
"exponent": &UnaryBuiltin{name: "exponent", function: builtinExponent, parameters: ast.Identifiers{"x"}},
|
||||||
"pow": &BinaryBuiltin{name: "pow", function: builtinPow, parameters: ast.Identifiers{"base", "exp"}},
|
"pow": &BinaryBuiltin{name: "pow", function: builtinPow, parameters: ast.Identifiers{"base", "exp"}},
|
||||||
"modulo": &BinaryBuiltin{name: "modulo", function: builtinModulo, parameters: ast.Identifiers{"x", "y"}},
|
"modulo": &BinaryBuiltin{name: "modulo", function: builtinModulo, parameters: ast.Identifiers{"x", "y"}},
|
||||||
|
"md5": &UnaryBuiltin{name: "md5", function: builtinMd5, parameters: ast.Identifiers{"x"}},
|
||||||
}
|
}
|
||||||
|
1
testdata/std.md5.golden
vendored
Normal file
1
testdata/std.md5.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
"f561aaf6ef0bf14d4208bb46a4ccb3ad"
|
1
testdata/std.md5.input
vendored
Normal file
1
testdata/std.md5.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5("xxx")
|
1
testdata/std.md5_2.golden
vendored
Normal file
1
testdata/std.md5_2.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
"d41d8cd98f00b204e9800998ecf8427e"
|
1
testdata/std.md5_2.input
vendored
Normal file
1
testdata/std.md5_2.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5("")
|
1
testdata/std.md5_3.golden
vendored
Normal file
1
testdata/std.md5_3.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
"0cc175b9c0f1b6a831c399e269772661"
|
1
testdata/std.md5_3.input
vendored
Normal file
1
testdata/std.md5_3.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5("a")
|
1
testdata/std.md5_4.golden
vendored
Normal file
1
testdata/std.md5_4.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
"f96b697d7cb7938d525a2f31aaf161d0"
|
1
testdata/std.md5_4.input
vendored
Normal file
1
testdata/std.md5_4.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5("message digest")
|
1
testdata/std.md5_5.golden
vendored
Normal file
1
testdata/std.md5_5.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
"5786eab716295401c073064c3ec82a44"
|
1
testdata/std.md5_5.input
vendored
Normal file
1
testdata/std.md5_5.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5("ą")
|
1
testdata/std.md5_6.golden
vendored
Normal file
1
testdata/std.md5_6.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
RUNTIME ERROR: Unexpected type number, expected string
|
1
testdata/std.md5_6.input
vendored
Normal file
1
testdata/std.md5_6.input
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
std.md5(42)
|
Loading…
Reference in New Issue
Block a user