mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-13 01:20:35 +02:00
Implement FROM_BASE64 function (#115)
This commit is contained in:
31
pkg/stdlib/strings/decode.go
Normal file
31
pkg/stdlib/strings/decode.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package strings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the value of a base64 representation.
|
||||||
|
* @param base64String (String) - The string to decode.
|
||||||
|
* @returns value (String) - The decoded string.
|
||||||
|
*/
|
||||||
|
func FromBase64(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||||
|
err := core.ValidateArgs(args, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
return values.EmptyString, err
|
||||||
|
}
|
||||||
|
|
||||||
|
value := args[0].String()
|
||||||
|
|
||||||
|
out, err := base64.StdEncoding.DecodeString(value)
|
||||||
|
if err != nil {
|
||||||
|
return values.EmptyString, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return values.NewString(string(out)), nil
|
||||||
|
}
|
46
pkg/stdlib/strings/decode_test.go
Normal file
46
pkg/stdlib/strings/decode_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package strings_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/stdlib/strings"
|
||||||
|
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFromBase64(t *testing.T) {
|
||||||
|
Convey("When args are not passed", t, func() {
|
||||||
|
Convey("It should return an error", func() {
|
||||||
|
var err error
|
||||||
|
_, err = strings.FromBase64(context.Background())
|
||||||
|
|
||||||
|
So(err, ShouldBeError)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("When hash is not valid base64", t, func() {
|
||||||
|
Convey("It should return an error", func() {
|
||||||
|
var err error
|
||||||
|
_, err = strings.FromBase64(
|
||||||
|
context.Background(),
|
||||||
|
values.NewString("foobar"),
|
||||||
|
)
|
||||||
|
|
||||||
|
So(err, ShouldBeError)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Should decode a given hash", t, func() {
|
||||||
|
out, err := strings.FromBase64(
|
||||||
|
context.Background(),
|
||||||
|
values.NewString("Zm9vYmFy"),
|
||||||
|
)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(out, ShouldNotEqual, "Zm9vYmFy")
|
||||||
|
So(out, ShouldEqual, "foobar")
|
||||||
|
})
|
||||||
|
}
|
@ -31,6 +31,7 @@ func NewLib() map[string]core.Function {
|
|||||||
"SUBSTITUTE": Substitute,
|
"SUBSTITUTE": Substitute,
|
||||||
"SUBSTRING": Substring,
|
"SUBSTRING": Substring,
|
||||||
"TO_BASE64": ToBase64,
|
"TO_BASE64": ToBase64,
|
||||||
|
"FROM_BASE64": FromBase64,
|
||||||
"TRIM": Trim,
|
"TRIM": Trim,
|
||||||
"UPPER": Upper,
|
"UPPER": Upper,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user