XML conflict fields

84 views
Skip to first unread message

Eugene Toropov

unread,
Jul 15, 2015, 5:00:26 AM7/15/15
to gola...@googlegroups.com
Привет,


Как можно изменить этот код, чтоб он различал xsi:type=“ProgressivePromotion” и type=“Percent”? В идеале хочу на выходе получить main.Discount{XsiType:” ProgressivePromotion”, Type:”Percent", From:"2015-07-28T00:00:00", To:"2015-07-30T00:00:00"}

Евгений

Айнар Гарипов

unread,
Jul 15, 2015, 8:32:31 AM7/15/15
to gola...@googlegroups.com
Хм. Как мне казалось, такое:

type Discount struct {
   
XSIType string `xml:"xsi type,attr"`
   
Type    string `xml:"type,attr"`
   
From    string `xml:"from,attr"`
   
To      string `xml:"to,attr"`
}


должно было работать. Но анмаршал ругается:

error: main.Discount field "XSIType" with tag "xsi type,attr" conflicts with field "Type" with tag "type,attr"

Судя по тому, что это уже чинили для элементов (в https://github.com/golang/go/issues/3703), похоже на баг. Отправил https://github.com/golang/go/issues/11724.

Пока могу лишь предложить вот такой костыль:

type Discount struct {
   
XSIType string `xml:"xsi type_,attr"`
   
Type    string `xml:"type,attr"`
   
From    string `xml:"from,attr"`
   
To      string `xml:"to,attr"`
}

// ...

s
:= `<Discount xsi:type="ProgressivePromotion" from="2015-07-28T00:00:00" to="2015-07-30T00:00:00" type="Percent" value="38" name="Special Deal" />`
s
= strings.Replace(s, " xsi:type=", " xsi:type_=", -1)

Либо как-то ещё переименовать type, если есть доступ к тому, откуда идёт XML.

Eugene Toropov

unread,
Jul 15, 2015, 8:49:11 AM7/15/15
to gola...@googlegroups.com
Попробую replace пока, спасибо.

--
Вы получили это сообщение, поскольку подписаны на группу "Golang Russian".
Чтобы отменить подписку на эту группу и больше не получать от нее сообщения, отправьте письмо на электронный адрес golang-ru+...@googlegroups.com.
Чтобы настроить другие параметры, перейдите по ссылке https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages