Skip to content

Commit

Permalink
Implement FROM_BASE64 function (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pangoraw authored and ziflex committed Oct 14, 2018
1 parent 0288798 commit 5db8df5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/stdlib/strings/decode.go
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions pkg/stdlib/strings/decode_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
1 change: 1 addition & 0 deletions pkg/stdlib/strings/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewLib() map[string]core.Function {
"SUBSTITUTE": Substitute,
"SUBSTRING": Substring,
"TO_BASE64": ToBase64,
"FROM_BASE64": FromBase64,
"TRIM": Trim,
"UPPER": Upper,
}
Expand Down

0 comments on commit 5db8df5

Please sign in to comment.