-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_rule.go
53 lines (43 loc) · 886 Bytes
/
list_rule.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
package overloading
import (
"errors"
"reflect"
)
type ListRule struct {
args []*Argument
op int
}
func NewListRule(args ...*Argument) *ListRule {
r := new(ListRule)
r.args = make([]*Argument, len(args))
for i, a := range args {
r.args[i] = a
if a.IsOptional() {
r.op += 1
}
}
return r
}
func (r *ListRule) Check(args []interface{}) ([]interface{}, error) {
_len := len(r.args)
out := make([]interface{}, _len)
n := 0
for i, e := range args {
if i >= _len {
return out, errors.New("Argument length is too long")
}
t := reflect.TypeOf(e).String()
if t != r.args[i].Type() {
return out, errors.New("Wrong argument type " + t + " at " + string(i))
}
out[i] = e
n++
}
if n < _len-r.op || _len < n {
return out, errors.New("Wrong argument length")
}
for i := n; i < _len; i++ {
out[i] = r.args[i].Default()
}
return out, nil
}