-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FROM_BASE64 function (#115)
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters