/
action.go
140 lines (124 loc) · 3.96 KB
/
action.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
// Copyright (C) 2015 NTT Innovation Institute, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schema
import (
"fmt"
"regexp"
"sort"
)
// Action struct
type Action struct {
ID string
Method string
Path string
Description string
InputSchema map[string]interface{}
OutputSchema map[string]interface{}
Parameters map[string]interface{}
}
// NewAction create Action
func NewAction(id, method, path, description string, inputSchema, outputSchema, parameters map[string]interface{}) Action {
return Action{
ID: id,
Method: method,
Path: path,
Description: description,
InputSchema: inputSchema,
OutputSchema: outputSchema,
Parameters: parameters,
}
}
// NewActionFromObject create Action object from json
func NewActionFromObject(id string, rawData interface{}) (Action, error) {
actionData, _ := rawData.(map[string]interface{})
method, _ := actionData["method"].(string)
path, _ := actionData["path"].(string)
description, _ := actionData["description"].(string)
inputSchema, _ := actionData["input"].(map[string]interface{})
outputSchema, _ := actionData["output"].(map[string]interface{})
parameters, _ := actionData["parameters"].(map[string]interface{})
return NewAction(id, method, path, description, inputSchema, outputSchema, parameters), nil
}
// TakesID checks if action takes ID as a parameter
func (action *Action) TakesID() bool {
re := regexp.MustCompile(`.*/:id(/.*)?$`)
return len(re.FindString(action.Path)) != 0
}
// GetInputType gets action input type
func (action *Action) GetInputType() (string, error) {
if action.InputSchema == nil {
return "", fmt.Errorf("Action does not take input")
}
inputType, ok := action.InputSchema["type"].(string)
if !ok {
return "", fmt.Errorf("Input schema does not have a type")
}
return inputType, nil
}
// GetInputParameterNames gets action input parameter names
func (action *Action) GetInputParameterNames() ([]string, error) {
if action.InputSchema == nil {
return nil, fmt.Errorf("Action does not take input")
}
properties, ok := action.InputSchema["properties"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Input schema does not have properties")
}
keys := make([]string, len(properties))
i := 0
for k := range properties {
keys[i] = k
i++
}
return keys, nil
}
// GetInputParameterType gets input parameter type
func (action *Action) GetInputParameterType(parameter string) (string, error) {
if action.InputSchema == nil {
return "", fmt.Errorf("Action does not take input")
}
properties, ok := action.InputSchema["properties"].(map[string]interface{})
if !ok {
return "", fmt.Errorf("Input schema does not have properties")
}
parameterObject, ok := properties[parameter].(map[string]interface{})
if !ok {
return "", fmt.Errorf("Property with ID %s not found", parameter)
}
parameterType, ok := parameterObject["type"].(string)
if !ok {
return "", fmt.Errorf("Parameter with ID %s does not have a type", parameter)
}
return parameterType, nil
}
// TakesNoArgs checks if action takes no arguments
func (action *Action) TakesNoArgs() bool {
return action.InputSchema == nil && !action.TakesID()
}
// Sorting
type actions []Action
func (a actions) Len() int {
return len(a)
}
func (a actions) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a actions) Less(i, j int) bool {
return a[i].ID < a[j].ID
}
// SortActions sort actions by id
func SortActions(schema *Schema) {
sort.Sort(actions(schema.Actions))
}