-
Notifications
You must be signed in to change notification settings - Fork 0
/
factories.go
116 lines (101 loc) · 3.15 KB
/
factories.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
package replacers
import (
"github.com/cubahno/connexions/config"
"github.com/cubahno/connexions/internal"
"github.com/cubahno/connexions/openapi"
"github.com/jaswdr/faker"
"reflect"
)
// ValueReplacer is a function that replaces value in schema or content.
// This function should encapsulate all the logic, data, contexts etc. of replacing values.
type ValueReplacer func(schemaOrContent any, state *ReplaceState) any
type ReplaceContext struct {
// Schema is a schema that is used to replace values.
// Currently only OpenAPI Schema is supported.
// It does not depend on schema provider as this is already converted to internal Schema type.
Schema any
// State is a state of the current replace operation.
// It is used to store information about the current element, including its name, index, content type etc.
State *ReplaceState
// AreaPrefix is a prefix that is used to identify the correct section
// in the context config for specific replacement area.
// e.g. in-
// then in the contexts we should have:
// in-header:
// X-GeneratedRequest-ID: 123
// in-path:
// user_id: 123
AreaPrefix string
// Data is a list of contexts that are used to replace values.
Data []map[string]any
// Faker is a faker instance that is used to generate fake data.
Faker faker.Faker
}
// Replacers is a list of replacers that are used to replace values in schemas and contents in the specified order.
var Replacers = []Replacer{
ReplaceInHeaders,
ReplaceInPath,
ReplaceFromContext,
ReplaceFromSchemaFormat,
ReplaceFromSchemaPrimitive,
ReplaceFromSchemaExample,
ReplaceFromSchemaFallback,
}
// CreateValueReplacer is a factory that creates a new ValueReplacer instance from the given config and contexts.
func CreateValueReplacer(cfg *config.Config, replacers []Replacer, contexts []map[string]any) ValueReplacer {
return func(content any, state *ReplaceState) any {
if state == nil {
state = NewReplaceState()
}
ctx := &ReplaceContext{
Schema: content,
State: state,
// initialize faker here, global var is racy
Faker: faker.New(),
AreaPrefix: cfg.App.ContextAreaPrefix,
Data: contexts,
}
for _, fn := range replacers {
res := fn(ctx)
if res != nil && ctx.Schema != nil {
if !HasCorrectSchemaValue(ctx, res) {
continue
}
res = ApplySchemaConstraints(ctx.Schema, res)
}
if res == nil {
continue
}
// return nil if function suggests
if str, ok := res.(string); ok {
if str == NULL {
return nil
}
}
return res
}
return nil
}
}
// IsCorrectlyReplacedType checks if the given value is of the correct schema type.
func IsCorrectlyReplacedType(value any, neededType string) bool {
switch neededType {
case openapi.TypeString:
_, ok := value.(string)
return ok
case openapi.TypeInteger:
return internal.IsInteger(value)
case openapi.TypeNumber:
return internal.IsNumber(value)
case openapi.TypeBoolean:
_, ok := value.(bool)
return ok
case openapi.TypeObject:
return reflect.TypeOf(value).Kind() == reflect.Map
case openapi.TypeArray:
val := reflect.ValueOf(value)
return val.Kind() == reflect.Slice || val.Kind() == reflect.Array
default:
return false
}
}