forked from credondocr/dota2api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.go
152 lines (134 loc) · 2.94 KB
/
parameters.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package dota2api
import (
"errors"
"fmt"
)
type parameterKind int
const (
parameterKindHeroId parameterKind = iota
parameterKindMatchesRequested
parameterKindAccountId
parameterKindStartAtMatchId
parameterKindMinPlayers
parameterKindMatchId
parameterStartMatchAtSeqNum
parameterSteamIds
parameterSteamId
parameterVanityUrl
)
func (p parameterKind) String() string {
switch p {
case parameterKindHeroId:
return "hero Id"
case parameterKindMatchesRequested:
return "matches requested"
case parameterKindAccountId:
return "account Id"
case parameterKindStartAtMatchId:
return "start at match Id"
case parameterKindMinPlayers:
return "min players"
case parameterKindMatchId:
return "match Id"
case parameterStartMatchAtSeqNum:
return "start match at seq num"
case parameterSteamIds:
return "steam Ids"
case parameterSteamId:
return "steam Id"
case parameterVanityUrl:
return "vanity Url"
}
return "unknown"
}
type Parameter interface {
key() string
value() interface{}
kind() parameterKind
}
func getParameterMap(require []parameterKind, accept []parameterKind, params []Parameter) (map[string]interface{}, error) {
for i, p := range params {
for j := i + 1; j < len(params); j++ {
if p.kind() == params[j].kind() {
return nil, errors.New(fmt.Sprintf("duplicate parameter \"%s\"", p.kind().String()))
}
}
}
m := make(map[string]interface{})
reqState := make([]bool, len(require))
reqFound := 0
var unaccepted []string
paramLoop:
for _, p := range params {
for i, r := range require {
if p.kind() == r {
reqState[i] = true
reqFound++
m[p.key()] = p.value()
continue paramLoop
}
}
for _, a := range accept {
if p.kind() == a {
m[p.key()] = p.value()
continue paramLoop
}
}
unaccepted = append(unaccepted, p.kind().String())
}
if unaccepted != nil {
return nil, errors.New(fmt.Sprintf("unaccepted parameter(s) %v", unaccepted))
}
if reqFound != len(require) {
var missing []string
for i, p := range reqState {
if !p {
missing = append(missing, require[i].String())
}
}
return nil, errors.New(fmt.Sprintf("missing required parameter(s) %v", missing))
}
return m, nil
}
type ParameterInt struct {
k string
v int
kindInt parameterKind
}
func (p ParameterInt) key() string {
return p.k
}
func (p ParameterInt) value() interface{} {
return p.v
}
func (p ParameterInt) kind() parameterKind {
return p.kindInt
}
type ParameterString struct {
k string
v string
kindInt parameterKind
}
func (p ParameterString) key() string {
return p.k
}
func (p ParameterString) value() interface{} {
return p.v
}
func (p ParameterString) kind() parameterKind {
return p.kindInt
}
type ParameterInt64 struct {
k string
v int64
kindInt parameterKind
}
func (p ParameterInt64) key() string {
return p.k
}
func (p ParameterInt64) value() interface{} {
return p.v
}
func (p ParameterInt64) kind() parameterKind {
return p.kindInt
}