-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathabs.yml
More file actions
150 lines (115 loc) · 2.91 KB
/
abs.yml
File metadata and controls
150 lines (115 loc) · 2.91 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
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
name: abs
topic: math
args:
required:
- name: value
type: number
desc: positive or negative number
returns:
type: number
desc: positive magnitude of input value
desc: |
Returns the absolute value of the provided argument. The signature indicates
that a number is returned, and that the input argument `$value` **must**
resolve to a number, otherwise a `invalid-type` error is triggered.
Below is a worked example. Given:
```
{"foo": -1, "bar": "2"}
```
Evaluating `abs(foo)` works as follows:
1. Evaluate the input argument against the current data:
```
search(foo, {"foo": -1, "bar": "2"}) -> -1
```
2. Validate the type of the resolved argument. In this case
`-1` is of type `number` so it passes the type check.
3. Call the function with the resolved argument:
```
abs(-1) -> 1
```
4. The value of `1` is the resolved value of the function expression
`abs(foo)`.
Below is the same steps for evaluating `abs(bar)`:
1. Evaluate the input argument against the current data:
```
search(bar, {"foo": -1, "bar": "2"}) -> "2"
```
2. Validate the type of the resolved argument. In this case
`"2"` is of type `string` so we immediately indicate that
an `invalid-type` error occurred.
As a final example, here is the steps for evaluating `abs(to_number(bar))`:
1. Evaluate the input argument against the current data:
```
search(to_number(bar), {"foo": -1, "bar": "2"})
```
2. In order to evaluate the above expression, we need to evaluate
`to_number(bar)`:
```
search(bar, {"foo": -1, "bar": "2"}) -> "2"
# Validate "2" passes the type check for to_number, which it does.
to_number("2") -> 2
```
Note that to_number is defined below.
3. Now we can evaluate the original expression:
```
search(to_number(bar), {"foo": -1, "bar": "2"}) -> 2
```
4. Call the function with the final resolved value:
```
abs(2) -> 2
```
5. The value of `2` is the resolved value of the function expression
`abs(to_number(bar))`.
examples:
test0:
context:
args: [1]
returns: 1
test1:
context:
args: [-1]
returns: 1
test2:
context:
args: [abc]
error: invalid-type
test3:
context: &data
foo: -1
zero: 0
numbers: [-1, 3, 4, 5]
array: [-1, 3, 4, 5, a, '100']
strings: [a, b, c]
decimals: [1.01, 1.2, -1.5]
str: Str
'false': false
empty_list: []
empty_hash: {}
objects: {foo: bar, bar: baz}
null_key:
args: [foo]
returns: 1
test4:
context: *data
args: [str]
error: invalid-type
test5:
context: *data
args: ['array[1]']
returns: 3
test6:
context: *data
args: [false]
error: invalid-type
test7:
context: *data
args: [-24]
returns: 24
test8:
context: *data
args: [1, 2]
error: invalid-arity
test9:
context: *data
args: ['']
error: invalid-arity