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
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (e *base64Encoder) PrintDocumentSeparator(_ io.Writer) error {
|
2022-02-22 22:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (e *base64Encoder) PrintLeadingContent(_ io.Writer, _ string) error {
|
2022-02-22 22:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (e *base64Encoder) Encode(writer io.Writer, node *CandidateNode) error {
|
2024-03-12 04:45:08 +00:00
|
|
|
if node.GuessTagFromCustomType() != "!!str" {
|
2024-02-24 05:07:15 +00:00
|
|
|
return fmt.Errorf("cannot encode %v as base64, can only operate on strings", node.Tag)
|
2022-02-22 22:26:35 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(node.Value))))
|
2022-02-22 22:26:35 +00:00
|
|
|
return err
|
|
|
|
}
|