warn about attribute change

This commit is contained in:
Mike Farah 2022-10-21 20:46:26 +11:00
parent 9546eb8939
commit 55383b9ce1
3 changed files with 29 additions and 3 deletions

View File

@ -47,11 +47,18 @@ yq -P sample.json
if verbose { if verbose {
backend.SetLevel(logging.DEBUG, "") backend.SetLevel(logging.DEBUG, "")
} else { } else {
backend.SetLevel(logging.ERROR, "") backend.SetLevel(logging.WARNING, "")
} }
logging.SetBackend(backend) logging.SetBackend(backend)
yqlib.InitExpressionParser() 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.")
}
}, },
} }

View File

@ -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 { func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) error {
// make sure <?xml .. ?> 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)) err := e.encodeComment(encoder, headAndLineComment(node))
if err != nil { if err != nil {
return err return err
@ -92,7 +109,9 @@ func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) er
return err 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) name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1)
procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)} procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)}
if err := encoder.EncodeToken(procInst); err != nil { if err := encoder.EncodeToken(procInst); err != nil {

View File

@ -35,7 +35,7 @@ type xmlPreferences struct {
func NewDefaultXmlPreferences() xmlPreferences { func NewDefaultXmlPreferences() xmlPreferences {
return xmlPreferences{ return xmlPreferences{
AttributePrefix: "+@", AttributePrefix: "+",
ContentName: "+content", ContentName: "+content",
StrictMode: false, StrictMode: false,
KeepNamespace: true, KeepNamespace: true,