ajson icon indicating copy to clipboard operation
ajson copied to clipboard

Add new functions `b64encode` and `b4decode`

Open spyzhov opened this issue 2 years ago • 1 comments

Add new functions b64encode and b64decode that are equal to the same functions from encoding/base64

spyzhov avatar Apr 22 '22 13:04 spyzhov

Select the name for method:

  • b64encode
  • base64_encode
  • encode_base64
  • encodeB64
  • b64_encode
  • etc.

spyzhov avatar May 13 '22 08:05 spyzhov

Hi @spyzhov can I work on this issue? I am fairly new to Golang, so it could take some time.

ryzheboka avatar Oct 23 '22 17:10 ryzheboka

@ryzheboka, sure! Please create 2 new methods with names base64_encode and base64_decode.

You can implement them in the same way as function avg (source: https://github.com/spyzhov/ajson/blob/master/math.go#L363-L380).

Input and Output type should be String only. Please cover them with tests in the math_test.go file.

spyzhov avatar Oct 23 '22 18:10 spyzhov

Do I declare the methods using the following signatures?

"base64_encode": func(node *Node) (result *Node, err error) { ... }

"base64_decode": func(node *Node) (result *Node, err error) { ... }

where the each Node is a string Node? What do I do if the Node isn't a string? Or should I rather use the following signature?

"base64_encode": func(input string) (result string, err error) { ... }

ryzheboka avatar Oct 24 '22 15:10 ryzheboka

The first suggestion was right, it should be something like this:

		"base64_encode": func(node *Node) (result *Node, err error) {
			if node.IsString() {
				if res, err := node.GetString(); err != nil {
					return nil, err
				} else {
					var result string
					// ... implementation
					return valueNode(nil, "base64_encode", String, result), nil
				}
			}
			return valueNode(nil, "base64_encode", Null, nil), nil
		},
		"base64_decode": func(node *Node) (result *Node, err error) {

The current implementation of functions could have only 1 argument and it's applicable to any type of node.

spyzhov avatar Oct 24 '22 19:10 spyzhov

Also, please be aware that base64 can be configured with/without padding:

_, _ = base64.StdEncoding.WithPadding(base64.NoPadding).DecodeString(str)
_, _ = base64.StdEncoding.WithPadding(base64.StdPadding).DecodeString(str)

Some strings can not be decoded with/without padding.

Probably better to create 4 functions: pair of functions with and without StdPadding(=)

spyzhov avatar Nov 03 '22 16:11 spyzhov

There seems to be a problem with https://github.com/mattn/goveralls which leads to the pipeline failure, The pipeline cannot execute go get github.com/mattn/goveralls. Here is the problem: https://app.travis-ci.com/github/spyzhov/ajson/jobs/587506014. Is it related to my PR, or is it a different problem?

ryzheboka avatar Nov 04 '22 11:11 ryzheboka

It's OK, when I'll press a button to run it "from my name", it will resolve this problem.

spyzhov avatar Nov 06 '22 20:11 spyzhov