-
Notifications
You must be signed in to change notification settings - Fork 8
/
unwrap_no_multiplex.go
77 lines (66 loc) · 1.66 KB
/
unwrap_no_multiplex.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
package runner
import (
"github.com/b2wdigital/restQL-golang/v5/internal/domain"
)
// UnwrapNoMultiplex transform a collection of unresolved Resources
// with `no-multiplex` functions into a collection of Resources
// without it.
func UnwrapNoMultiplex(resources domain.Resources) domain.Resources {
for resourceID, resource := range resources {
resources[resourceID] = unwrapResource(resource)
}
return resources
}
func unwrapResource(resource interface{}) interface{} {
switch resource := resource.(type) {
case domain.Statement:
return unwrapStatement(resource)
case []interface{}:
multiplexedResource := make([]interface{}, len(resource))
for i, r := range resource {
multiplexedResource[i] = unwrapResource(r)
}
return multiplexedResource
default:
return resource
}
}
func unwrapStatement(statement domain.Statement) domain.Statement {
params := statement.With.Values
for key, value := range params {
result := unwrapValue(value)
params[key] = result
}
body := unwrapBody(statement.With.Body)
statement.With.Body = body
statement.With.Values = params
return statement
}
func unwrapBody(body interface{}) interface{} {
switch body := body.(type) {
case domain.NoMultiplex:
return body.Target()
default:
return body
}
}
func unwrapValue(value interface{}) interface{} {
switch value := value.(type) {
case domain.NoMultiplex:
return value.Target()
case map[string]interface{}:
m := make(map[string]interface{})
for k, v := range value {
m[k] = unwrapValue(v)
}
return m
case []interface{}:
l := make([]interface{}, len(value))
for i, v := range value {
l[i] = unwrapValue(v)
}
return l
default:
return value
}
}