2021-07-25 01:43:51 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2022-01-15 00:57:59 +00:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2021-07-25 01:43:51 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-07-27 11:51:27 +00:00
|
|
|
"strings"
|
2021-07-25 01:43:51 +00:00
|
|
|
|
|
|
|
"github.com/magiconair/properties"
|
|
|
|
)
|
|
|
|
|
|
|
|
type propertiesEncoder struct {
|
2022-06-25 02:22:03 +00:00
|
|
|
unwrapScalar bool
|
2024-02-19 23:57:44 +00:00
|
|
|
prefs PropertiesPreferences
|
2021-07-25 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 23:57:44 +00:00
|
|
|
func NewPropertiesEncoder(unwrapScalar bool, prefs PropertiesPreferences) Encoder {
|
2022-06-25 02:22:03 +00:00
|
|
|
return &propertiesEncoder{
|
|
|
|
unwrapScalar: unwrapScalar,
|
2024-02-19 23:57:44 +00:00
|
|
|
prefs: prefs,
|
2022-06-25 02:22:03 +00:00
|
|
|
}
|
2021-07-25 01:43:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (pe *propertiesEncoder) CanHandleAliases() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (pe *propertiesEncoder) PrintDocumentSeparator(_ io.Writer) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pe *propertiesEncoder) PrintLeadingContent(writer io.Writer, content string) error {
|
|
|
|
reader := bufio.NewReader(strings.NewReader(content))
|
|
|
|
for {
|
|
|
|
|
|
|
|
readline, errReading := reader.ReadString('\n')
|
|
|
|
if errReading != nil && !errors.Is(errReading, io.EOF) {
|
|
|
|
return errReading
|
|
|
|
}
|
2023-09-18 23:52:36 +00:00
|
|
|
if strings.Contains(readline, "$yqDocSeparator$") {
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
if err := pe.PrintDocumentSeparator(writer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if err := writeString(writer, readline); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(errReading, io.EOF) {
|
|
|
|
if readline != "" {
|
2023-03-16 02:39:36 +00:00
|
|
|
// the last comment we read didn't have a newline, put one in
|
2022-01-15 00:57:59 +00:00
|
|
|
if err := writeString(writer, "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (pe *propertiesEncoder) Encode(writer io.Writer, node *CandidateNode) error {
|
2023-03-27 02:51:08 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if node.Kind == ScalarNode {
|
2023-03-27 02:51:08 +00:00
|
|
|
return writeString(writer, node.Value+"\n")
|
|
|
|
}
|
|
|
|
|
2021-07-25 01:43:51 +00:00
|
|
|
mapKeysToStrings(node)
|
|
|
|
p := properties.NewProperties()
|
2024-02-19 23:57:44 +00:00
|
|
|
p.WriteSeparator = pe.prefs.KeyValueSeparator
|
2022-10-28 03:16:46 +00:00
|
|
|
err := pe.doEncode(p, node, "", nil)
|
2021-07-25 01:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
_, err = p.WriteComment(writer, "#", properties.UTF8)
|
2021-07-25 01:43:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *CandidateNode, path string, keyNode *CandidateNode) error {
|
2022-10-28 03:16:46 +00:00
|
|
|
|
|
|
|
comments := ""
|
|
|
|
if keyNode != nil {
|
|
|
|
// include the key node comments if present
|
|
|
|
comments = headAndLineComment(keyNode)
|
|
|
|
}
|
|
|
|
comments = comments + headAndLineComment(node)
|
|
|
|
commentsWithSpaces := strings.ReplaceAll(comments, "\n", "\n ")
|
|
|
|
p.SetComments(path, strings.Split(commentsWithSpaces, "\n"))
|
|
|
|
|
2021-07-25 01:43:51 +00:00
|
|
|
switch node.Kind {
|
2023-10-18 01:11:53 +00:00
|
|
|
case ScalarNode:
|
2022-06-25 02:22:03 +00:00
|
|
|
var nodeValue string
|
|
|
|
if pe.unwrapScalar || !strings.Contains(node.Value, " ") {
|
|
|
|
nodeValue = node.Value
|
|
|
|
} else {
|
|
|
|
nodeValue = fmt.Sprintf("%q", node.Value)
|
|
|
|
}
|
|
|
|
_, _, err := p.Set(path, nodeValue)
|
2021-07-25 08:08:33 +00:00
|
|
|
return err
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
2021-07-25 01:43:51 +00:00
|
|
|
return pe.encodeArray(p, node.Content, path)
|
2023-10-18 01:11:53 +00:00
|
|
|
case MappingNode:
|
2021-07-25 01:43:51 +00:00
|
|
|
return pe.encodeMap(p, node.Content, path)
|
2023-10-18 01:11:53 +00:00
|
|
|
case AliasNode:
|
2022-10-28 03:16:46 +00:00
|
|
|
return pe.doEncode(p, node.Alias, path, nil)
|
2021-07-25 01:43:51 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported node %v", node.Tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pe *propertiesEncoder) appendPath(path string, key interface{}) string {
|
|
|
|
if path == "" {
|
|
|
|
return fmt.Sprintf("%v", key)
|
|
|
|
}
|
2024-02-20 00:39:56 +00:00
|
|
|
switch key.(type) {
|
|
|
|
case int:
|
|
|
|
if pe.prefs.UseArrayBrackets {
|
|
|
|
return fmt.Sprintf("%v[%v]", path, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-07-25 01:43:51 +00:00
|
|
|
return fmt.Sprintf("%v.%v", path, key)
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (pe *propertiesEncoder) encodeArray(p *properties.Properties, kids []*CandidateNode, path string) error {
|
2021-07-25 01:43:51 +00:00
|
|
|
for index, child := range kids {
|
2022-10-28 03:16:46 +00:00
|
|
|
err := pe.doEncode(p, child, pe.appendPath(path, index), nil)
|
2021-07-25 01:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (pe *propertiesEncoder) encodeMap(p *properties.Properties, kids []*CandidateNode, path string) error {
|
2021-07-25 01:43:51 +00:00
|
|
|
for index := 0; index < len(kids); index = index + 2 {
|
|
|
|
key := kids[index]
|
|
|
|
value := kids[index+1]
|
2022-10-28 03:16:46 +00:00
|
|
|
err := pe.doEncode(p, value, pe.appendPath(path, key.Value), key)
|
2021-07-25 01:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|