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