Skip to content

Commit c2e2ff4

Browse files
committed
Issue #4: recursivity with fieldmappings with an object type
1 parent 6780309 commit c2e2ff4

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

pkg/quickwit/quickwit.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ type QuickwitDatasource struct {
2626
dsInfo es.DatasourceInfo
2727
}
2828

29+
type FieldMappings struct {
30+
Name string `json:"name"`
31+
Type string `json:"type"`
32+
OutputFormat *string `json:"output_format,omitempty"`
33+
FieldMappings []FieldMappings `json:"field_mappings,omitempty"`
34+
}
35+
2936
type QuickwitMapping struct {
3037
IndexConfig struct {
3138
DocMapping struct {
32-
TimestampField string `json:"timestamp_field"`
33-
FieldMappings []struct {
34-
Name string `json:"name"`
35-
Type string `json:"type"`
36-
OutputFormat *string `json:"output_format,omitempty"`
37-
} `json:"field_mappings"`
39+
TimestampField string `json:"timestamp_field"`
40+
FieldMappings []FieldMappings `json:"field_mappings"`
3841
} `json:"doc_mapping"`
3942
} `json:"index_config"`
4043
}
@@ -56,6 +59,22 @@ func newErrorCreationPayload(statusCode int, message string) error {
5659
return errors.New(string(json))
5760
}
5861

62+
func findTimeStampFormat(timestampFieldName string, fieldMappings []FieldMappings) *string {
63+
if nil == fieldMappings {
64+
return nil
65+
}
66+
67+
for _, field := range fieldMappings {
68+
if field.Type == "datetime" && field.Name == timestampFieldName && nil != field.OutputFormat {
69+
return field.OutputFormat
70+
} else if field.Type == "object" && nil != field.FieldMappings {
71+
return findTimeStampFormat(timestampFieldName, field.FieldMappings)
72+
}
73+
}
74+
75+
return nil
76+
}
77+
5978
func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (string, string, error) {
6079
mappingEndpointUrl := qwUrl + "/indexes/" + index
6180
qwlog.Info("Calling quickwit endpoint: " + mappingEndpointUrl)
@@ -91,22 +110,16 @@ func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
91110
}
92111

93112
timestampFieldName := payload.IndexConfig.DocMapping.TimestampField
94-
timestampFieldFormat := "undef"
95-
for _, field := range payload.IndexConfig.DocMapping.FieldMappings {
96-
if field.Type == "datetime" && field.Name == timestampFieldName && nil != field.OutputFormat {
97-
timestampFieldFormat = *field.OutputFormat
98-
break
99-
}
100-
}
113+
timestampFieldFormat := findTimeStampFormat(timestampFieldName, payload.IndexConfig.DocMapping.FieldMappings)
101114

102-
if timestampFieldFormat == "undef" {
115+
if nil == timestampFieldFormat {
103116
errMsg := fmt.Sprintf("No format found for field: %s", string(timestampFieldName))
104117
qwlog.Error(errMsg)
105118
return timestampFieldName, "", newErrorCreationPayload(statusCode, errMsg)
106119
}
107120

108-
qwlog.Info(fmt.Sprintf("Found timestampFieldName = %s, timestampFieldFormat = %s", timestampFieldName, timestampFieldFormat))
109-
return timestampFieldName, timestampFieldFormat, nil
121+
qwlog.Info(fmt.Sprintf("Found timestampFieldName = %s, timestampFieldFormat = %s", timestampFieldName, *timestampFieldFormat))
122+
return timestampFieldName, *timestampFieldFormat, nil
110123
}
111124

112125
// Creates a Quickwit datasource.

0 commit comments

Comments
 (0)