This commit is contained in:
Stanisław Barzowski 2017-09-13 10:39:19 -04:00 committed by Dave Cunningham
parent 999ff267f1
commit 8a80309137
13 changed files with 24 additions and 0 deletions

View File

@ -18,6 +18,8 @@ package jsonnet
import (
"bytes"
"crypto/md5"
"encoding/hex"
"math"
"sort"
@ -373,6 +375,15 @@ func builtinType(e *evaluator, xp potentialValue) (value, error) {
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) {
if math.IsNaN(x) {
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"}},
"pow": &BinaryBuiltin{name: "pow", function: builtinPow, parameters: ast.Identifiers{"base", "exp"}},
"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
View File

@ -0,0 +1 @@
"f561aaf6ef0bf14d4208bb46a4ccb3ad"

1
testdata/std.md5.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5("xxx")

1
testdata/std.md5_2.golden vendored Normal file
View File

@ -0,0 +1 @@
"d41d8cd98f00b204e9800998ecf8427e"

1
testdata/std.md5_2.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5("")

1
testdata/std.md5_3.golden vendored Normal file
View File

@ -0,0 +1 @@
"0cc175b9c0f1b6a831c399e269772661"

1
testdata/std.md5_3.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5("a")

1
testdata/std.md5_4.golden vendored Normal file
View File

@ -0,0 +1 @@
"f96b697d7cb7938d525a2f31aaf161d0"

1
testdata/std.md5_4.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5("message digest")

1
testdata/std.md5_5.golden vendored Normal file
View File

@ -0,0 +1 @@
"5786eab716295401c073064c3ec82a44"

1
testdata/std.md5_5.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5("ą")

1
testdata/std.md5_6.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: Unexpected type number, expected string

1
testdata/std.md5_6.input vendored Normal file
View File

@ -0,0 +1 @@
std.md5(42)