Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package yqlib | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/url" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
type uriDecoder struct { | ||
reader io.Reader | ||
finished bool | ||
readAnything bool | ||
} | ||
|
||
func NewUriDecoder() Decoder { | ||
return &uriDecoder{finished: false} | ||
} | ||
|
||
func (dec *uriDecoder) Init(reader io.Reader) error { | ||
dec.reader = reader | ||
dec.readAnything = false | ||
dec.finished = false | ||
return nil | ||
} | ||
|
||
func (dec *uriDecoder) Decode() (*CandidateNode, error) { | ||
if dec.finished { | ||
return nil, io.EOF | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
if _, err := buf.ReadFrom(dec.reader); err != nil { | ||
return nil, err | ||
} | ||
if buf.Len() == 0 { | ||
dec.finished = true | ||
|
||
// if we've read _only_ an empty string, lets return that | ||
// otherwise if we've already read some bytes, and now we get | ||
// an empty string, then we are done. | ||
if dec.readAnything { | ||
return nil, io.EOF | ||
} | ||
} | ||
newValue, err := url.QueryUnescape(buf.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dec.readAnything = true | ||
return &CandidateNode{ | ||
Node: &yaml.Node{ | ||
Kind: yaml.ScalarNode, | ||
Tag: "!!str", | ||
Value: newValue, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package yqlib | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
type uriEncoder struct { | ||
} | ||
|
||
func NewUriEncoder() Encoder { | ||
return &uriEncoder{} | ||
} | ||
|
||
func (e *uriEncoder) CanHandleAliases() bool { | ||
return false | ||
} | ||
|
||
func (e *uriEncoder) PrintDocumentSeparator(writer io.Writer) error { | ||
return nil | ||
} | ||
|
||
func (e *uriEncoder) PrintLeadingContent(writer io.Writer, content string) error { | ||
return nil | ||
} | ||
|
||
func (e *uriEncoder) Encode(writer io.Writer, originalNode *yaml.Node) error { | ||
node := unwrapDoc(originalNode) | ||
if guessTagFromCustomType(node) != "!!str" { | ||
return fmt.Errorf("cannot encode %v as url, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
_, err := writer.Write([]byte(url.QueryEscape(originalNode.Value))) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,9 @@ var participleYqRules = []*participleYqRule{ | |
{"Base64d", `@base64d`, decodeOp(Base64InputFormat), 0}, | ||
{"Base64", `@base64`, encodeWithIndent(Base64OutputFormat, 0), 0}, | ||
|
||
{"Urld", `@urid`, decodeOp(UriInputFormat), 0}, | ||
{"Url", `@uri`, encodeWithIndent(UriOutputFormat, 0), 0}, | ||
This comment has been minimized.
Sorry, something went wrong.
hhromic
|
||
|
||
{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0}, | ||
|
||
{"LoadBase64", `load_?base64`, loadOp(NewBase64Decoder(), false), 0}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 6d7d76a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for reviewing! I'll make those changes
Noticed another inconsistency here. Shouldn't it be
Cannot encode %v as URI, ...
?