yq/pkg/xml/xml.go

68 lines
1.8 KiB
Go
Raw Normal View History

2024-03-12 04:45:08 +00:00
package xml
import "github.com/mikefarah/yq/v4/pkg/yqlib"
2022-10-25 03:27:16 +00:00
type XmlPreferences struct {
2024-02-24 04:03:30 +00:00
Indent int
2022-10-25 03:27:16 +00:00
AttributePrefix string
ContentName string
StrictMode bool
KeepNamespace bool
UseRawToken bool
ProcInstPrefix string
DirectiveName string
SkipProcInst bool
SkipDirectives bool
}
func NewDefaultXmlPreferences() XmlPreferences {
return XmlPreferences{
2024-02-24 04:03:30 +00:00
Indent: 2,
2022-11-10 11:22:55 +00:00
AttributePrefix: "+@",
2022-10-25 03:27:16 +00:00
ContentName: "+content",
StrictMode: false,
KeepNamespace: true,
UseRawToken: true,
2022-10-25 03:27:16 +00:00
ProcInstPrefix: "+p_",
DirectiveName: "+directive",
SkipProcInst: false,
SkipDirectives: false,
}
}
2024-02-24 04:03:30 +00:00
func (p *XmlPreferences) Copy() XmlPreferences {
return XmlPreferences{
Indent: p.Indent,
AttributePrefix: p.AttributePrefix,
ContentName: p.ContentName,
StrictMode: p.StrictMode,
KeepNamespace: p.KeepNamespace,
UseRawToken: p.UseRawToken,
ProcInstPrefix: p.ProcInstPrefix,
DirectiveName: p.DirectiveName,
SkipProcInst: p.SkipProcInst,
SkipDirectives: p.SkipDirectives,
}
}
2022-10-25 03:27:16 +00:00
var ConfiguredXMLPreferences = NewDefaultXmlPreferences()
2024-03-12 04:45:08 +00:00
var XMLFormat = &yqlib.Format{"xml", []string{"x"},
func() yqlib.Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) },
func() yqlib.Decoder { return NewXMLDecoder(ConfiguredXMLPreferences) },
}
var xmlYqRules = []*yqlib.ParticipleYqRule{
{"XMLEncodeWithIndent", `to_?xml\([0-9]+\)`, encodeParseIndent(XMLFormat), 0},
{"XmlDecode", `from_?xml|@xmld`, decodeOp(XMLFormat), 0},
{"XMLEncode", `to_?xml`, encodeWithIndent(XMLFormat, 2), 0},
{"XMLEncodeNoIndent", `@xml`, encodeWithIndent(XMLFormat, 0), 0},
{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0},
}
func RegisterXmlFormat() {
yqlib.RegisterFormat(XMLFormat)
yqlib.RegisterRules(xmlYqRules)
}