Skip to content

Commit c91c393

Browse files
committed
add fields mappings in config
1 parent a33344d commit c91c393

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

pkg/genlib/config/config.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package config
22

33
import (
44
"errors"
5+
"regexp"
6+
"sort"
7+
"strings"
58
"time"
69

710
"math"
@@ -38,8 +41,94 @@ type Config struct {
3841
m map[string]ConfigField
3942
}
4043

44+
// HasFieldsMappings checks if all fields have a type defined.
45+
// This is useful to determine if a fields definitions file is required.
46+
func (c Config) HasFieldsMappings() bool {
47+
for _, f := range c.m {
48+
if f.Type == "" {
49+
return false
50+
}
51+
}
52+
return true
53+
}
54+
55+
// FieldMapping represents the mapping of a field.
56+
// It defines the structure and properties of a field, including its name,
57+
// data type, associated object type, example value, and the actual value.
58+
type FieldMapping struct {
59+
Name string
60+
Type string
61+
ObjectType string
62+
Example string
63+
Value any
64+
}
65+
66+
// FieldsMappings is a collection of FieldMapping.
67+
type FieldsMappings []FieldMapping
68+
69+
func (f FieldsMappings) Len() int { return len(f) }
70+
func (f FieldsMappings) Less(i, j int) bool { return f[i].Name < f[j].Name }
71+
func (f FieldsMappings) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
72+
73+
func normaliseFields(fields FieldsMappings) (FieldsMappings, error) {
74+
sort.Sort(fields)
75+
normalisedFields := make(FieldsMappings, 0, len(fields))
76+
for _, field := range fields {
77+
if !strings.Contains(field.Name, "*") {
78+
normalisedFields = append(normalisedFields, field)
79+
continue
80+
}
81+
82+
normalizationPattern := strings.NewReplacer(".", "\\.", "*", ".+").Replace(field.Name)
83+
re, err := regexp.Compile(normalizationPattern)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
hasMatch := false
89+
for _, otherField := range fields {
90+
if otherField.Name == field.Name {
91+
continue
92+
}
93+
94+
if re.MatchString(otherField.Name) {
95+
hasMatch = true
96+
break
97+
}
98+
}
99+
100+
if !hasMatch {
101+
normalisedFields = append(normalisedFields, field)
102+
}
103+
}
104+
105+
sort.Sort(normalisedFields)
106+
return normalisedFields, nil
107+
}
108+
109+
// LoadFieldsMappings creates the fields mappings from the config itself.
110+
// It has to be called after the config is loaded.
111+
func (c Config) LoadFieldsMappings() (FieldsMappings, error) {
112+
var mappings FieldsMappings
113+
114+
for _, f := range c.m {
115+
mappings = append(mappings, FieldMapping{
116+
Name: f.Name,
117+
Type: f.Type,
118+
ObjectType: f.ObjectType,
119+
Example: f.Example,
120+
Value: f.Value,
121+
})
122+
}
123+
124+
return normaliseFields(mappings)
125+
}
126+
41127
type ConfigField struct {
42128
Name string `config:"name"`
129+
Type string `config:"type"`
130+
ObjectType string `config:"object_type"`
131+
Example string `config:"example"`
43132
Fuzziness float64 `config:"fuzziness"`
44133
Range Range `config:"range"`
45134
Cardinality int `config:"cardinality"`

0 commit comments

Comments
 (0)