From 55383b9ce1bee9644d6b9da821867720d629b37b Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 21 Oct 2022 20:46:26 +1100 Subject: [PATCH] warn about attribute change --- cmd/root.go | 9 ++++++++- pkg/yqlib/encoder_xml.go | 21 ++++++++++++++++++++- pkg/yqlib/lib.go | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2f802b33..3a8a7621 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,11 +47,18 @@ yq -P sample.json if verbose { backend.SetLevel(logging.DEBUG, "") } else { - backend.SetLevel(logging.ERROR, "") + backend.SetLevel(logging.WARNING, "") } logging.SetBackend(backend) yqlib.InitExpressionParser() + if (inputFormat == "x" || inputFormat == "xml") && + outputFormat != "x" && outputFormat != "xml" && + yqlib.XMLPreferences.AttributePrefix == "+" { + yqlib.GetLogger().Warning("The default xml-attribute-prefix will change in the next release to `+@` to avoid " + + "naming conflicts with the default content name, directive name and proc inst prefix. If you need to keep " + + "`+` please set that value explicityly with --xml-attribute-prefix.") + } }, } diff --git a/pkg/yqlib/encoder_xml.go b/pkg/yqlib/encoder_xml.go index 4e3dd151..012e64b7 100644 --- a/pkg/yqlib/encoder_xml.go +++ b/pkg/yqlib/encoder_xml.go @@ -77,6 +77,23 @@ func (e *xmlEncoder) Encode(writer io.Writer, node *yaml.Node) error { } func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) error { + // make sure processing instructions are encoded first + for i := 0; i < len(node.Content); i += 2 { + key := node.Content[i] + value := node.Content[i+1] + + if key.Value == (e.prefs.ProcInstPrefix + "xml") { + name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1) + procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)} + if err := encoder.EncodeToken(procInst); err != nil { + return err + } + if _, err := e.writer.Write([]byte("\n")); err != nil { + log.Warning("Unable to write newline, skipping: %w", err) + } + } + } + err := e.encodeComment(encoder, headAndLineComment(node)) if err != nil { return err @@ -92,7 +109,9 @@ func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) er return err } - if strings.HasPrefix(key.Value, e.prefs.ProcInstPrefix) { + if key.Value == (e.prefs.ProcInstPrefix + "xml") { + // dont double process these. + } else if strings.HasPrefix(key.Value, e.prefs.ProcInstPrefix) { name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1) procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)} if err := encoder.EncodeToken(procInst); err != nil { diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 18383383..910ab801 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -35,7 +35,7 @@ type xmlPreferences struct { func NewDefaultXmlPreferences() xmlPreferences { return xmlPreferences{ - AttributePrefix: "+@", + AttributePrefix: "+", ContentName: "+content", StrictMode: false, KeepNamespace: true,