I want to create structs = each type of command.
Commands have common part of xml - CommandResult. I created interface Command. I need to SomeCommand implements Command and can to be parsed as xml, also IsError must be realized in CommandResult, other functions must be realized by SomeCommand.
Code:
type Command interface {
IsError() bool
Request(buf *bufio.Writer, params interface{}) error
...
}
// Result of request
type CommandResult struct {
Code int `xml:"code,attr" json:"code"`
Message string `xml:"msg" json:"msg"`
}
// this Command's func is realized by CommandResult
func (self CommandResult) IsError() bool {
return true
}
// some command
type SomeCommand struct {
CommandResult // CommandResult `xml:"response>result" json:"result"`
}
// this Command's func is realized by SomeCommand
func (self SomeCommand) Request(buf *bufio.Writer, params interface{}) error {
return nil
}
// other Command's functions are realized by CommandResult too
XML:
<epp>
<response>
<result code="1000">
<msg>Command completed successfully</msg>
</result>
<trID>
<svTRID>asd</svTRID>
</trID>
</response>
</epp>
Expected result:
a := SomeCommand
xml.NewDecoder(reader).Decode(&a)
// a.CommandResult.Code = 1000
// a.CommandResult.Message = 'Command completed successfully'
// a implements Command
I think paths in embedded structure should be absolute as all "parent's" structures are part of the "child". So your
Should be more like
BUT! There we are facing Go's limitation.
You can't use
attr
with chaining. There is issue on Github but looks like it is not in priority list. So if I right understand shortest version of yourCommandResult
declaration would be:Not a real problem but in case if you will decide to convert
Command
back to XML would be nice to declare itsXMLName
. Something likeBecause without it XML encoder will produce something like
<SomeCommand><response>...</response></SomeCommand>
Update with full example