-
Notifications
You must be signed in to change notification settings - Fork 0
/
calltable.go
120 lines (98 loc) · 2.47 KB
/
calltable.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"fmt"
"net/url"
"strings"
rest "github.com/ktnyt/go-rest"
"github.com/mitchellh/mapstructure"
)
// CallData holds individual called data.
type CallData struct {
Called string `json:"called"`
Remark string `json:"remark"`
}
// Calltable is the model for calltable
type Calltable struct {
ID string `json:"id"`
Caller *string `json:"caller, omitempty"`
Callee *string `json:"callee, omitempty"`
Data []CallData `json:"data"`
}
// NewCalltable creates a new empty Calltable.
func NewCalltable() rest.Model {
return &Calltable{}
}
// Validate the calltable entry fields
func (c *Calltable) Validate() error {
missing := make([]string, 0)
if c.Caller == nil {
missing = append(missing, "'caller'")
}
if c.Callee == nil {
missing = append(missing, "'callee'")
}
if c.Data == nil {
missing = append(missing, "'data'")
}
if len(missing) > 0 {
return fmt.Errorf("Bad Request: %s", strings.Join(missing, ", "))
}
return nil
}
// MakeKey for a new calltable entry
func (c *Calltable) MakeKey(i int) string {
c.ID = *c.Caller + *c.Callee
return c.ID
}
// Merge another calltable entry into this calltable entry
func (c *Calltable) Merge(m interface{}) error {
other := m.(*Calltable)
if other.Caller != nil {
c.Caller = other.Caller
}
if other.Callee != nil {
c.Callee = other.Callee
}
if other.Data != nil {
c.Data = other.Data
}
return nil
}
// CalltableFilter applies the given filter to the model.
func CalltableFilter(ctx context.Context) rest.Filter {
return func(value interface{}) bool {
params := ctx.Value(rest.Params).(url.Values)
caller := params.Get("caller")
callee := params.Get("callee")
calltable := value.(*Calltable)
if len(caller) > 0 {
if *calltable.Caller != caller {
return false
}
}
if len(callee) > 0 {
if *calltable.Callee != callee {
return false
}
}
return true
}
}
// CalltableConverter converts a value to a Model.
func CalltableConverter(value interface{}) rest.Model {
calltable := new(Calltable)
mapstructure.Decode(value, &calltable)
return calltable
}
// NewCalltableService creates a default Calltable Service.
func NewCalltableService() rest.Service {
return rest.NewDictService(NewCalltable, CalltableFilter, CalltableConverter)
}
func init() {
name := "calltable"
handler := NewFileIOHandler(name+".db", NewCalltableService)
service := rest.NewIOService(handler)
iface := rest.NewServiceInterface(service)
register(name, router, iface)
}