1

Не могу разобраться как сделать атрибут: SOAP-ENC:arrayType="cwmp:ParameterValueStruct[2]", для переменной что бы при маршалинге оно выглядело вот так:

<ParameterList
                    SOAP-ENC:arrayType="cwmp:ParameterValueStruct[2]">
                <ParameterValueStruct>
                    <Name>
                        InternetGatewayDevice.DeviceSummary
                    </Name>
                    <Value
                            xsi:type="xsd:string">
                        InternetGatewayDevice
                    </Value>
                </ParameterValueStruct>
                <ParameterValueStruct>
                    <Name>
                        InternetGatewayDevice.DeviceInfo.SpecVersion
                    </Name>
                    <Value
                            xsi:type="xsd:string">
                        1.0
                    </Value>
                </ParameterValueStruct>
</ParameterList>

2 ответа 2

0

Оказалось костыльно все. Что бы создать атрибут для массива необходимо создать структуру с именем структуры и в ней хранить атрибут и массив:

type Inform struct {
    ParameterList *ParameterList
}

type ParameterList struct {
    XMLAttr string `xml:"SOAP-ENC:arrayType,attr"`
    ParameterValueStruct []*ParameterValueStruct
}

type ParameterValueStruct struct {
    Name string
    Value string
}

Что бы передать атрибуту значение необходимо сделать так:

inf := &structures.Inform{}
inf.ParameterList = &structures.ParameterList{
        ParameterValueStruct:[]*structures.ParameterValueStruct{
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
            &structures.ParameterValueStruct{},
        },
    }

    inf.ParameterList.XMLAttr = fmt.Sprintf("cwmp:ParameterValueStruct[%d]", len(inf.ParameterList.ParameterValueStruct))
0

Это делается с помощью простого определения метода MarshalXML для списка параметров:

type ParameterList struct {
    Parameters []*ParameterValueStruct `xml:"ParameterValueStruct"`
}

func (pl ParameterList) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
    start.Attr = append(start.Attr, xml.Attr{
        Name: xml.Name{
            Local: "arrayType",
            Space: "http://schemas.xmlsoap.org/soap/encoding/",
        },
        Value: fmt.Sprintf("cwmp:ParameterValueStruct[%d]", len(pl.Parameters)),
    })
    type pure ParameterList
    return enc.EncodeElement(pure(pl), start)
}

Playground: https://play.golang.org/p/HFTTgsTqtyj.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.