| 1 | package xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 5 | "encoding/xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/" |
| 6 | "errorsscip-go gomod github.com/golang/go/src go1.25 errors/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 8 | "ioscip-go gomod github.com/golang/go/src go1.25 io/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25 strings/" |
| 10 | |
| 11 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 12 | "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/" |
| 13 | ) |
| 14 | |
| 15 | // Security limits for XML parsing to prevent DoS attacks. |
| 16 | // These limits are intentionally conservative to balance usability and safety. |
| 17 | const ( |
| 18 | maxCommentLengthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxCommentLength. = 10_000 // Maximum bytes per comment (10KB) - prevents memory exhaustion from single large comments |
| 19 | maxTotalCommentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxTotalComments. = 1_000 // Maximum comments per document - prevents abuse via comment flooding |
| 20 | maxXMLSizescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxXMLSize. = 10_000_000 // Maximum XML input size (10MB) - prevents processing of excessively large files |
| 21 | maxXMLDepthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxXMLDepth. = 10_000 // Maximum element nesting depth - prevents stack overflow from deeply nested documents |
| 22 | ) |
| 23 | |
| 24 | // ErrXMLMaxDepthExceeded is returned when XML nesting depth exceeds maxXMLDepth. |
| 25 | var ErrXMLMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/ErrXMLMaxDepthExceeded. = errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Newscip-go gomod github.com/golang/go/src go1.25 errors/New().("xml nesting depth exceeded") |
| 26 | |
| 27 | func newXMLReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/newXMLReader().(options parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.ReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/ReaderOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Readerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#, error) { |
| 28 | return &xmlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#{ |
| 29 | structuredscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#structured.: options.Extscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/ReaderOptions#Ext.["xml-mode"] == "structured", |
| 30 | }, nil |
| 31 | } |
| 32 | |
| 33 | type xmlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader# struct { |
| 34 | structuredscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#structured. bool |
| 35 | } |
| 36 | |
| 37 | // Read reads a value from a byte slice. |
| 38 | func (j *xmlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#) Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#Read().(data []byte) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 39 | if len(data) > maxXMLSizescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxXMLSize. { |
| 40 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("XML input exceeds maximum size of %d bytes", maxXMLSizescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxXMLSize.) |
| 41 | } |
| 42 | |
| 43 | decoder := xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25 bytes/NewReader().(data)) |
| 44 | decoder.Strictscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Decoder#Strict. = true |
| 45 | |
| 46 | totalComments := 0 |
| 47 | el, err := j.parseElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#parseElement().(decoder, xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.StartElementscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#{ |
| 48 | Namescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#Name.: xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.Namescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Name#{ |
| 49 | Localscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Name#Local.: "root", |
| 50 | }, |
| 51 | }, &totalComments, 0) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | if j.structuredscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#structured. { |
| 57 | return el.toStructuredModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toStructuredModel().() |
| 58 | } |
| 59 | return el.toFriendlyModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toFriendlyModel().() |
| 60 | } |
| 61 | |
| 62 | func (e *xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#) toStructuredModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toStructuredModel().() (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 63 | attrs := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 64 | for _, attr := range e.Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs. { |
| 65 | if err := attrs.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(attr.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Name., modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(attr.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Value.)); err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | } |
| 69 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 70 | if len(e.ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions.) > 0 { |
| 71 | res.SetMetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMetadataValue().("xml_processing_instructions", e.ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions.) |
| 72 | } |
| 73 | if len(e.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.) > 0 { |
| 74 | res.SetMetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMetadataValue().("xml_comments", e.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.) |
| 75 | } |
| 76 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("name", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(e.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.)); err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("attrs", attrs); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("content", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(e.Contentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Content.)); err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | children := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 87 | for _, child := range e.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children. { |
| 88 | childModel, err := child.toStructuredModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toStructuredModel().() |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | if err := children.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(childModel); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | } |
| 96 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("children", children); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | return res, nil |
| 100 | } |
| 101 | |
| 102 | func (e *xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#) toFriendlyModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toFriendlyModel().() (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 103 | if len(e.Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs.) == 0 && len(e.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children.) == 0 && len(e.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.) == 0 { |
| 104 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(e.Contentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Content.), nil |
| 105 | } |
| 106 | |
| 107 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 108 | if len(e.ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions.) > 0 { |
| 109 | res.SetMetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMetadataValue().("xml_processing_instructions", e.ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions.) |
| 110 | } |
| 111 | if len(e.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.) > 0 { |
| 112 | res.SetMetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMetadataValue().("xml_comments", e.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.) |
| 113 | } |
| 114 | for _, attr := range e.Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs. { |
| 115 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("-"+attr.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Name., modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(attr.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Value.)); err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if len(e.Contentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Content.) > 0 { |
| 121 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("#text", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(e.Contentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Content.)); err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if len(e.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children.) > 0 { |
| 127 | childElementKeys := make([]string, 0) |
| 128 | childElements := make(map[string][]*xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#) |
| 129 | // childOrder records document-order child names for round-trip preservation. |
| 130 | childOrder := make([]string, 0, len(e.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children.)) |
| 131 | |
| 132 | for _, child := range e.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children. { |
| 133 | if _, ok := childElements[child.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.]; !ok { |
| 134 | childElementKeys = append(childElementKeys, child.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.) |
| 135 | } |
| 136 | childElements[child.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.] = append(childElements[child.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.], child) |
| 137 | childOrder = append(childOrder, child.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.) |
| 138 | } |
| 139 | |
| 140 | for _, key := range childElementKeys { |
| 141 | cs := childElements[key] |
| 142 | switch len(cs) { |
| 143 | case 0: |
| 144 | continue |
| 145 | case 1: |
| 146 | childModel, err := cs[0].toFriendlyModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toFriendlyModel().() |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key, childModel); err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | default: |
| 154 | children := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 155 | for _, child := range cs { |
| 156 | childModel, err := child.toFriendlyModelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#toFriendlyModel().() |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | if err := children.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(childModel); err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | } |
| 164 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key, children); err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | res.SetMetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMetadataValue().(xmlChildOrderKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlChildOrderKey., childOrder) |
| 171 | } |
| 172 | |
| 173 | return res, nil |
| 174 | } |
| 175 | |
| 176 | func (j *xmlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#) parseElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#parseElement().(decoder *xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.Decoderscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Decoder#, element xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.StartElementscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#, totalComments *int, depth int) (*xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#, error) { |
| 177 | if depth > maxXMLDepthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxXMLDepth. { |
| 178 | return nil, ErrXMLMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/ErrXMLMaxDepthExceeded. |
| 179 | } |
| 180 | |
| 181 | el := &xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#{ |
| 182 | Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name.: element.Namescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#Name..Localscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Name#Local., |
| 183 | Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs.: make([]xmlAttrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#, 0), |
| 184 | Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children.: make([]*xmlElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#, 0), |
| 185 | ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions.: make([]*xmlProcessingInstructionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlProcessingInstruction#, 0), |
| 186 | Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments.: make([]*xmlCommentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlComment#, 0), |
| 187 | } |
| 188 | |
| 189 | for _, attr := range element.Attrscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#Attr. { |
| 190 | el.Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs. = append(el.Attrsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Attrs., xmlAttrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#{ |
| 191 | Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Name.: attr.Namescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Attr#Name..Localscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Name#Local., |
| 192 | Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlAttr#Value.: attr.Valuescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Attr#Value., |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | var processingInstructions []*xmlProcessingInstructionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlProcessingInstruction# |
| 197 | var comments []*xmlCommentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlComment# |
| 198 | |
| 199 | for { |
| 200 | t, err := decoder.Tokenscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Decoder#Token().() |
| 201 | if errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Isscip-go gomod github.com/golang/go/src go1.25 errors/Is().(err, ioscip-go gomod github.com/golang/go/src go1.25 io/.EOFscip-go gomod github.com/golang/go/src go1.25 io/EOF.) { |
| 202 | if el.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name. == "root" { |
| 203 | return el, nil |
| 204 | } |
| 205 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected EOF") |
| 206 | } |
| 207 | if err != nil { |
| 208 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to read token: %w", err) |
| 209 | } |
| 210 | if t == nil { |
| 211 | if el.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Name. == "root" { |
| 212 | return el, nil |
| 213 | } |
| 214 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected nil token") |
| 215 | } |
| 216 | |
| 217 | switch t := t.(type) { |
| 218 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.StartElementscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/StartElement#: |
| 219 | child, err := j.parseElementscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlReader#parseElement().(decoder, t, totalComments, depth+1) |
| 220 | if err != nil { |
| 221 | return nil, err |
| 222 | } |
| 223 | if len(comments) > 0 { |
| 224 | child.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments. = append(comments, child.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments....) |
| 225 | comments = nil |
| 226 | } |
| 227 | if len(processingInstructions) > 0 { |
| 228 | child.ProcessingInstructionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#ProcessingInstructions. = processingInstructions |
| 229 | processingInstructions = nil |
| 230 | } |
| 231 | el.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children. = append(el.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Children., child) |
| 232 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.CharDatascip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/CharData#: |
| 233 | stringContent := string(t) |
| 234 | if stringsscip-go gomod github.com/golang/go/src go1.25 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25 strings/TrimSpace().(stringContent) == "" { |
| 235 | continue |
| 236 | } |
| 237 | el.Contentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Content. += stringContent |
| 238 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.EndElementscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/EndElement#: |
| 239 | if len(comments) > 0 { |
| 240 | el.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments. = append(el.Commentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlElement#Comments., comments...) |
| 241 | } |
| 242 | return el, nil |
| 243 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.Commentscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Comment#: |
| 244 | commentText := string(t) |
| 245 | if len(commentText) > maxCommentLengthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxCommentLength. { |
| 246 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("comment exceeds maximum length of %d bytes", maxCommentLengthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxCommentLength.) |
| 247 | } |
| 248 | if *totalComments >= maxTotalCommentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxTotalComments. { |
| 249 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("document exceeds maximum comment count of %d", maxTotalCommentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/maxTotalComments.) |
| 250 | } |
| 251 | comment := &xmlCommentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlComment#{ |
| 252 | Textscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlComment#Text.: commentText, |
| 253 | } |
| 254 | comments = append(comments, comment) |
| 255 | *totalComments++ |
| 256 | continue |
| 257 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.ProcInstscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/ProcInst#: |
| 258 | pi := &xmlProcessingInstructionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlProcessingInstruction#{ |
| 259 | Targetscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlProcessingInstruction#Target.: t.Targetscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/ProcInst#Target., |
| 260 | Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/xmlProcessingInstruction#Value.: string(t.Instscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/ProcInst#Inst.), |
| 261 | } |
| 262 | processingInstructions = append(processingInstructions, pi) |
| 263 | continue |
| 264 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.Directivescip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Directive#: |
| 265 | continue |
| 266 | case xmlscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/.Attrscip-go gomod github.com/golang/go/src go1.25 `encoding/xml`/Attr#: |
| 267 | continue |
| 268 | default: |
| 269 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %v", t) |
| 270 | } |
| 271 | } |
| 272 | } |