forked from freeconf/restconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire_format.go
59 lines (45 loc) · 1.64 KB
/
wire_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package restconf
import (
"fmt"
"io"
"github.com/freeconf/yang/meta"
)
func getWireFormatter(accept MimeType) wireFormat {
if accept.IsXml() {
return xmlWireFormat(0)
}
return jsonWireFormat(0)
}
type wireFormat interface {
writeNotificationStart(w io.Writer, module *meta.Module, etime string) (int, error)
writeNotificationEnd(w io.Writer) (int, error)
writeRpcOutputStart(w io.Writer, module *meta.Module) (int, error)
writeRpcOutputEnd(w io.Writer) (int, error)
}
type jsonWireFormat int
func (jsonWireFormat) writeNotificationStart(w io.Writer, module *meta.Module, etime string) (int, error) {
return fmt.Fprintf(w, `{"ietf-restconf:notification":{"eventTime":"%s","event":`, etime)
}
func (jsonWireFormat) writeNotificationEnd(w io.Writer) (int, error) {
return fmt.Fprint(w, "}}")
}
func (jsonWireFormat) writeRpcOutputStart(w io.Writer, module *meta.Module) (int, error) {
return fmt.Fprintf(w, `{"%s:output":`, module.Ident())
}
func (jsonWireFormat) writeRpcOutputEnd(w io.Writer) (int, error) {
return fmt.Fprint(w, "}")
}
type xmlWireFormat int
func (xmlWireFormat) writeNotificationStart(w io.Writer, module *meta.Module, etime string) (int, error) {
return fmt.Fprintf(w, `<notification xmlns="urn:ietf:params:xml:ns:netconf:notif\
ication:1.0"><eventTime>%s</eventTime><event xmlns="%s">`, etime, module.Namespace())
}
func (xmlWireFormat) writeNotificationEnd(w io.Writer) (int, error) {
return fmt.Fprint(w, "</event></notification>")
}
func (xmlWireFormat) writeRpcOutputStart(w io.Writer, module *meta.Module) (int, error) {
return 0, nil
}
func (xmlWireFormat) writeRpcOutputEnd(w io.Writer) (int, error) {
return 0, nil
}