Added base64 support

This commit is contained in:
Mike Farah
2022-02-23 09:26:35 +11:00
parent d7b158f855
commit d9bca65626
10 changed files with 218 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package yqlib
import (
"encoding/base64"
"fmt"
"io"
yaml "gopkg.in/yaml.v3"
)
type base64Encoder struct {
encoding base64.Encoding
}
func NewBase64Encoder() Encoder {
return &base64Encoder{encoding: *base64.StdEncoding}
}
func (e *base64Encoder) CanHandleAliases() bool {
return false
}
func (e *base64Encoder) PrintDocumentSeparator(writer io.Writer) error {
return nil
}
func (e *base64Encoder) PrintLeadingContent(writer io.Writer, content string) error {
return nil
}
func (e *base64Encoder) Encode(writer io.Writer, originalNode *yaml.Node) error {
node := unwrapDoc(originalNode)
if guessTagFromCustomType(node) != "!!str" {
return fmt.Errorf("cannot encode %v as base64, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag)
}
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(originalNode.Value))))
return err
}