Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
75 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
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,44 @@ | ||
package yqlib | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"regexp" | ||
"strings" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
var pattern = regexp.MustCompile(`[^\w@%+=:,./-]`) | ||
|
||
type shEncoder struct { | ||
} | ||
|
||
func NewShEncoder() Encoder { | ||
return &shEncoder{} | ||
} | ||
|
||
func (e *shEncoder) CanHandleAliases() bool { | ||
return false | ||
} | ||
|
||
func (e *shEncoder) PrintDocumentSeparator(writer io.Writer) error { | ||
return nil | ||
} | ||
|
||
func (e *shEncoder) PrintLeadingContent(writer io.Writer, content string) error { | ||
return nil | ||
} | ||
|
||
func (e *shEncoder) Encode(writer io.Writer, originalNode *yaml.Node) error { | ||
node := unwrapDoc(originalNode) | ||
if guessTagFromCustomType(node) != "!!str" { | ||
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) | ||
} | ||
|
||
value := originalNode.Value | ||
if pattern.MatchString(value) { | ||
value = "'" + strings.ReplaceAll(value, "'", "\\'") + "'" | ||
} | ||
return writeString(writer, value) | ||
} |
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
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