yq/pkg/yqlib/encoder_base64.go

36 lines
859 B
Go
Raw Normal View History

2022-02-22 22:26:35 +00:00
package yqlib
import (
"encoding/base64"
"fmt"
"io"
)
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
}
2023-06-07 17:45:42 +00:00
func (e *base64Encoder) Encode(writer io.Writer, node *CandidateNode) error {
2023-04-08 09:56:35 +00:00
if node.guessTagFromCustomType() != "!!str" {
2022-02-22 22:26:35 +00:00
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)
}
2023-06-07 17:45:42 +00:00
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(node.Value))))
2022-02-22 22:26:35 +00:00
return err
}