forked from rockorager/go-jmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvocation.go
More file actions
52 lines (47 loc) · 1.03 KB
/
invocation.go
File metadata and controls
52 lines (47 loc) · 1.03 KB
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
package jmap
import (
"encoding/json"
"fmt"
)
// An Invocation represents method calls and responses
type Invocation struct {
// The name of the method call or response
Name string
// Object containing the named arguments for the method or response
Args any
// Arbitrary string set by client, echoed back with responses
CallID string
}
func (i *Invocation) MarshalJSON() ([]byte, error) {
j := []any{
i.Name,
i.Args,
i.CallID,
}
return json.Marshal(j)
}
func (i *Invocation) UnmarshalJSON(data []byte) error {
raw := []json.RawMessage{}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
if len(raw) != 3 {
return fmt.Errorf("not enough values in invocation")
}
if err := json.Unmarshal(raw[0], &i.Name); err != nil {
return err
}
newFn, ok := methods[i.Name]
if !ok {
return fmt.Errorf("method '%s' not registered", i.Name)
}
i.Args = newFn()
if err := json.Unmarshal(raw[1], i.Args); err != nil {
return err
}
if err := json.Unmarshal(raw[2], &i.CallID); err != nil {
return err
}
return nil
}