Skip to content

Commit

Permalink
Set RPCReply.Ok based on presence of <ok/> tag in reply
Browse files Browse the repository at this point in the history
closes Juniper#34
  • Loading branch information
Wayne Tucker committed Feb 18, 2018
1 parent 97b43ac commit 036b425
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions netconf/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,23 @@ type RPCReply struct {
XMLName xml.Name `xml:"rpc-reply"`
Errors []RPCError `xml:"rpc-error,omitempty"`
Data string `xml:",innerxml"`
Ok bool `xml:",omitempty"`
Ok bool `xml:"ok,omitempty"`
RawReply string `xml:"-"`
}

func newRPCReply(rawXML []byte, ErrOnWarning bool) (*RPCReply, error) {
reply := &RPCReply{}
reply := &RPCReply{Ok: true}
reply.RawReply = string(rawXML)

if err := xml.Unmarshal(rawXML, reply); err != nil {
return nil, err
}

// ugly workaround for golang's XML unmarshaling of empty tags
// if <ok/> is missing then omitempty behavior makes Ok true
// if <ok/> is present then omitempty behavior makes OK the value of that tag (nil) which then translates to false
reply.Ok = !reply.Ok

if reply.Errors != nil {
for _, rpcErr := range reply.Errors {
if rpcErr.Severity == "error" || ErrOnWarning {
Expand Down
7 changes: 5 additions & 2 deletions netconf/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var RPCReplytests = []struct {
</commit-results>
<ok/>
</rpc-reply>`,
false,
true,
},
{
`
Expand Down Expand Up @@ -160,7 +160,7 @@ configuration check-out failed: (missing mandatory statements)
</commit-results>
<ok/>
</rpc-reply>`,
false,
true,
},
}

Expand All @@ -173,5 +173,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 036b425

Please sign in to comment.