yq/pkg/yqlib/encoder_uri.go

35 lines
738 B
Go
Raw Normal View History

2023-01-23 00:37:18 +00:00
package yqlib
import (
"fmt"
"io"
"net/url"
)
type uriEncoder struct {
}
func NewUriEncoder() Encoder {
return &uriEncoder{}
}
func (e *uriEncoder) CanHandleAliases() bool {
return false
}
2024-01-11 02:17:34 +00:00
func (e *uriEncoder) PrintDocumentSeparator(_ io.Writer) error {
2023-01-23 00:37:18 +00:00
return nil
}
2024-01-11 02:17:34 +00:00
func (e *uriEncoder) PrintLeadingContent(_ io.Writer, _ string) error {
2023-01-23 00:37:18 +00:00
return nil
}
func (e *uriEncoder) Encode(writer io.Writer, node *CandidateNode) error {
if node.guessTagFromCustomType() != "!!str" {
2023-01-25 00:18:48 +00:00
return fmt.Errorf("cannot encode %v as URI, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag)
2023-01-23 00:37:18 +00:00
}
_, err := writer.Write([]byte(url.QueryEscape(node.Value)))
2023-01-23 00:37:18 +00:00
return err
}