Skip to content

Commit

Permalink
Change RPCReply.Ok to a struct and set based on <ok/> tag
Browse files Browse the repository at this point in the history
Alternate implementation to work around XML unmarshaling limitations in
Golang.  I'm not a fan of this method since it breaks the API, but I'm
providing it for completeness.

For my money I'd rather see 8e66346
merged and then have the upstream XML module fixed to add a "flag"
field option.

closes Juniper#34
  • Loading branch information
Wayne Tucker committed Feb 19, 2018
1 parent 8dab2d1 commit b93817c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 12 additions & 1 deletion netconf/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type RPCReply struct {
XMLName xml.Name `xml:"rpc-reply"`
Errors []RPCError `xml:"rpc-error,omitempty"`
Data string `xml:",innerxml"`
Ok bool `xml:",omitempty"`
Ok RPCReplyOk `xml:"ok,omitempty"`
RawReply string `xml:"-"`
}

Expand All @@ -72,6 +72,17 @@ func newRPCReply(rawXML []byte, ErrOnWarning bool) (*RPCReply, error) {
return reply, nil
}

type RPCReplyOk struct {
bool
}

func (c *RPCReplyOk) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
*c = RPCReplyOk{true}
return nil
}

// RPCError defines an error reply to a RPC request
type RPCError struct {
Type string `xml:"error-type"`
Expand Down
11 changes: 7 additions & 4 deletions netconf/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestUUIDChar(t *testing.T) {

var RPCReplytests = []struct {
rawXML string
replyOk bool
replyOk RPCReplyOk
}{
{
`
Expand All @@ -158,7 +158,7 @@ var RPCReplytests = []struct {
</commit-results>
<ok/>
</rpc-reply>`,
false,
RPCReplyOk{true},
},
{
`
Expand All @@ -184,7 +184,7 @@ configuration check-out failed: (missing mandatory statements)
</rpc-error>
</commit-results>
</rpc-reply>`,
false,
RPCReplyOk{false},
},
{
`
Expand Down Expand Up @@ -213,7 +213,7 @@ configuration check-out failed: (missing mandatory statements)
</commit-results>
<ok/>
</rpc-reply>`,
false,
RPCReplyOk{true},
},
}

Expand All @@ -226,5 +226,8 @@ func TestNewRPCReply(t *testing.T) {
if reply.RawReply != tc.rawXML {
t.Errorf("newRPCReply(%q) did not set RawReply to input, got %q", tc.rawXML, reply.RawReply)
}
if reply.Ok != tc.replyOk {
t.Errorf("newRPCReply(%q).Ok == %v, want %v", tc.rawXML, reply.Ok, tc.replyOk)
}
}
}

0 comments on commit b93817c

Please sign in to comment.