MCPcopy Create free account
hub / github.com/XmirrorSecurity/OpenSCA-cli / marshalAttr

Method marshalAttr

opensca/sca/java/xml/marshal.go:574–655  ·  view source on GitHub ↗

marshalAttr marshals an attribute with the given name and value, adding to start.Attr.

(start *StartElement, name Name, val reflect.Value)

Source from the content-addressed store, hash-verified

572
573// marshalAttr marshals an attribute with the given name and value, adding to start.Attr.
574func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value) error {
575 if val.CanInterface() && val.Type().Implements(marshalerAttrType) {
576 attr, err := val.Interface().(MarshalerAttr).MarshalXMLAttr(name)
577 if err != nil {
578 return err
579 }
580 if attr.Name.Local != "" {
581 start.Attr = append(start.Attr, attr)
582 }
583 return nil
584 }
585
586 if val.CanAddr() {
587 pv := val.Addr()
588 if pv.CanInterface() && pv.Type().Implements(marshalerAttrType) {
589 attr, err := pv.Interface().(MarshalerAttr).MarshalXMLAttr(name)
590 if err != nil {
591 return err
592 }
593 if attr.Name.Local != "" {
594 start.Attr = append(start.Attr, attr)
595 }
596 return nil
597 }
598 }
599
600 if val.CanInterface() && val.Type().Implements(textMarshalerType) {
601 text, err := val.Interface().(encoding.TextMarshaler).MarshalText()
602 if err != nil {
603 return err
604 }
605 start.Attr = append(start.Attr, Attr{name, string(text)})
606 return nil
607 }
608
609 if val.CanAddr() {
610 pv := val.Addr()
611 if pv.CanInterface() && pv.Type().Implements(textMarshalerType) {
612 text, err := pv.Interface().(encoding.TextMarshaler).MarshalText()
613 if err != nil {
614 return err
615 }
616 start.Attr = append(start.Attr, Attr{name, string(text)})
617 return nil
618 }
619 }
620
621 // Dereference or skip nil pointer, interface values.
622 switch val.Kind() {
623 case reflect.Pointer, reflect.Interface:
624 if val.IsNil() {
625 return nil
626 }
627 val = val.Elem()
628 }
629
630 // Walk slices.
631 if val.Kind() == reflect.Slice && val.Type().Elem().Kind() != reflect.Uint8 {

Callers 1

marshalValueMethod · 0.95

Calls 3

marshalSimpleMethod · 0.95
MarshalXMLAttrMethod · 0.80
IndexMethod · 0.80

Tested by

no test coverage detected