From 37f00e3e280bf9868e611fd7a4addc6b67724deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Saint-F=C3=A9lix?= Date: Mon, 25 Mar 2024 16:57:15 +0100 Subject: [PATCH] Fixes #830 (#834) --- typedapi/types/arraycomparecondition.go | 26 +++++++++----- typedapi/types/datedecayfunction.go | 2 +- typedapi/types/geoboundingboxquery.go | 21 ++++++----- typedapi/types/geodecayfunction.go | 2 +- typedapi/types/geodistancequery.go | 21 ++++++----- typedapi/types/geodistancesort.go | 48 ++++++++++++------------- typedapi/types/geopolygonquery.go | 21 ++++++----- typedapi/types/geoshapequery.go | 21 ++++++----- typedapi/types/numericdecayfunction.go | 2 +- typedapi/types/shapequery.go | 21 ++++++----- typedapi/types/shardstore.go | 21 ++++++----- typedapi/types/sortoptions.go | 2 +- typedapi/types/termsquery.go | 21 ++++++----- 13 files changed, 127 insertions(+), 102 deletions(-) diff --git a/typedapi/types/arraycomparecondition.go b/typedapi/types/arraycomparecondition.go index 6bd898f5e4..7c0e43413b 100644 --- a/typedapi/types/arraycomparecondition.go +++ b/typedapi/types/arraycomparecondition.go @@ -35,7 +35,7 @@ import ( // // https://github.com/elastic/elasticsearch-specification/blob/00fd9ffbc085e011cce9deb05bab4feaaa6b4115/specification/watcher/_types/Conditions.ts#L32-L36 type ArrayCompareCondition struct { - ArrayCompareCondition map[conditionop.ConditionOp]ArrayCompareOpParams `json:"ArrayCompareCondition,omitempty"` + ArrayCompareCondition map[conditionop.ConditionOp]ArrayCompareOpParams `json:"-"` Path string `json:"path"` } @@ -54,14 +54,6 @@ func (s *ArrayCompareCondition) UnmarshalJSON(data []byte) error { switch t { - case "ArrayCompareCondition": - if s.ArrayCompareCondition == nil { - s.ArrayCompareCondition = make(map[conditionop.ConditionOp]ArrayCompareOpParams, 0) - } - if err := dec.Decode(&s.ArrayCompareCondition); err != nil { - return fmt.Errorf("%s | %w", "ArrayCompareCondition", err) - } - case "path": var tmp json.RawMessage if err := dec.Decode(&tmp); err != nil { @@ -76,6 +68,22 @@ func (s *ArrayCompareCondition) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.ArrayCompareCondition == nil { + s.ArrayCompareCondition = make(map[conditionop.ConditionOp]ArrayCompareOpParams, 0) + } + raw := NewArrayCompareOpParams() + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "ArrayCompareCondition", err) + } + enum := conditionop.ConditionOp{} + err := enum.UnmarshalText([]byte(key)) + if err != nil { + return fmt.Errorf("cannot unmarshal enum conditionop.ConditionOp: %w", err) + } + s.ArrayCompareCondition[enum] = *raw + } + } } return nil diff --git a/typedapi/types/datedecayfunction.go b/typedapi/types/datedecayfunction.go index c50e933018..f64c34821f 100644 --- a/typedapi/types/datedecayfunction.go +++ b/typedapi/types/datedecayfunction.go @@ -31,7 +31,7 @@ import ( // // https://github.com/elastic/elasticsearch-specification/blob/00fd9ffbc085e011cce9deb05bab4feaaa6b4115/specification/_types/query_dsl/compound.ts#L186-L188 type DateDecayFunction struct { - DateDecayFunction map[string]DecayPlacementDateMathDuration `json:"DateDecayFunction,omitempty"` + DateDecayFunction map[string]DecayPlacementDateMathDuration `json:"-"` // MultiValueMode Determines how the distance is calculated when a field used for computing the // decay contains multiple values. MultiValueMode *multivaluemode.MultiValueMode `json:"multi_value_mode,omitempty"` diff --git a/typedapi/types/geoboundingboxquery.go b/typedapi/types/geoboundingboxquery.go index 695e03502c..7fde4f68e1 100644 --- a/typedapi/types/geoboundingboxquery.go +++ b/typedapi/types/geoboundingboxquery.go @@ -42,7 +42,7 @@ type GeoBoundingBoxQuery struct { // A boost value between 0 and 1.0 decreases the relevance score. // A value greater than 1.0 increases the relevance score. Boost *float32 `json:"boost,omitempty"` - GeoBoundingBoxQuery map[string]GeoBounds `json:"GeoBoundingBoxQuery,omitempty"` + GeoBoundingBoxQuery map[string]GeoBounds `json:"-"` // IgnoreUnmapped Set to `true` to ignore an unmapped field and not match any documents for // this query. // Set to `false` to throw an exception if the field is not mapped. @@ -86,14 +86,6 @@ func (s *GeoBoundingBoxQuery) UnmarshalJSON(data []byte) error { s.Boost = &f } - case "GeoBoundingBoxQuery": - if s.GeoBoundingBoxQuery == nil { - s.GeoBoundingBoxQuery = make(map[string]GeoBounds, 0) - } - if err := dec.Decode(&s.GeoBoundingBoxQuery); err != nil { - return fmt.Errorf("%s | %w", "GeoBoundingBoxQuery", err) - } - case "ignore_unmapped": var tmp interface{} dec.Decode(&tmp) @@ -132,6 +124,17 @@ func (s *GeoBoundingBoxQuery) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.GeoBoundingBoxQuery == nil { + s.GeoBoundingBoxQuery = make(map[string]GeoBounds, 0) + } + raw := new(GeoBounds) + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "GeoBoundingBoxQuery", err) + } + s.GeoBoundingBoxQuery[key] = *raw + } + } } return nil diff --git a/typedapi/types/geodecayfunction.go b/typedapi/types/geodecayfunction.go index d030983c4f..5d2c23d969 100644 --- a/typedapi/types/geodecayfunction.go +++ b/typedapi/types/geodecayfunction.go @@ -31,7 +31,7 @@ import ( // // https://github.com/elastic/elasticsearch-specification/blob/00fd9ffbc085e011cce9deb05bab4feaaa6b4115/specification/_types/query_dsl/compound.ts#L190-L192 type GeoDecayFunction struct { - GeoDecayFunction map[string]DecayPlacementGeoLocationDistance `json:"GeoDecayFunction,omitempty"` + GeoDecayFunction map[string]DecayPlacementGeoLocationDistance `json:"-"` // MultiValueMode Determines how the distance is calculated when a field used for computing the // decay contains multiple values. MultiValueMode *multivaluemode.MultiValueMode `json:"multi_value_mode,omitempty"` diff --git a/typedapi/types/geodistancequery.go b/typedapi/types/geodistancequery.go index c80c6207ce..bd84370a5a 100644 --- a/typedapi/types/geodistancequery.go +++ b/typedapi/types/geodistancequery.go @@ -49,7 +49,7 @@ type GeoDistanceQuery struct { // Set to `plane` for a faster calculation that's inaccurate on long distances // and close to the poles. DistanceType *geodistancetype.GeoDistanceType `json:"distance_type,omitempty"` - GeoDistanceQuery map[string]GeoLocation `json:"GeoDistanceQuery,omitempty"` + GeoDistanceQuery map[string]GeoLocation `json:"-"` // IgnoreUnmapped Set to `true` to ignore an unmapped field and not match any documents for // this query. // Set to `false` to throw an exception if the field is not mapped. @@ -102,14 +102,6 @@ func (s *GeoDistanceQuery) UnmarshalJSON(data []byte) error { return fmt.Errorf("%s | %w", "DistanceType", err) } - case "GeoDistanceQuery": - if s.GeoDistanceQuery == nil { - s.GeoDistanceQuery = make(map[string]GeoLocation, 0) - } - if err := dec.Decode(&s.GeoDistanceQuery); err != nil { - return fmt.Errorf("%s | %w", "GeoDistanceQuery", err) - } - case "ignore_unmapped": var tmp interface{} dec.Decode(&tmp) @@ -143,6 +135,17 @@ func (s *GeoDistanceQuery) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.GeoDistanceQuery == nil { + s.GeoDistanceQuery = make(map[string]GeoLocation, 0) + } + raw := new(GeoLocation) + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "GeoDistanceQuery", err) + } + s.GeoDistanceQuery[key] = *raw + } + } } return nil diff --git a/typedapi/types/geodistancesort.go b/typedapi/types/geodistancesort.go index cc3ac64554..1e47043cdd 100644 --- a/typedapi/types/geodistancesort.go +++ b/typedapi/types/geodistancesort.go @@ -39,7 +39,7 @@ import ( // https://github.com/elastic/elasticsearch-specification/blob/00fd9ffbc085e011cce9deb05bab4feaaa6b4115/specification/_types/sort.ts#L58-L66 type GeoDistanceSort struct { DistanceType *geodistancetype.GeoDistanceType `json:"distance_type,omitempty"` - GeoDistanceSort map[string][]GeoLocation `json:"GeoDistanceSort,omitempty"` + GeoDistanceSort map[string][]GeoLocation `json:"-"` IgnoreUnmapped *bool `json:"ignore_unmapped,omitempty"` Mode *sortmode.SortMode `json:"mode,omitempty"` Order *sortorder.SortOrder `json:"order,omitempty"` @@ -66,31 +66,6 @@ func (s *GeoDistanceSort) UnmarshalJSON(data []byte) error { return fmt.Errorf("%s | %w", "DistanceType", err) } - case "GeoDistanceSort": - if s.GeoDistanceSort == nil { - s.GeoDistanceSort = make(map[string][]GeoLocation, 0) - } - rawMsg := make(map[string]json.RawMessage, 0) - dec.Decode(&rawMsg) - for key, value := range rawMsg { - switch { - case bytes.HasPrefix(value, []byte("\"")), bytes.HasPrefix(value, []byte("{")): - o := new(GeoLocation) - err := json.NewDecoder(bytes.NewReader(value)).Decode(&o) - if err != nil { - return fmt.Errorf("%s | %w", "GeoDistanceSort", err) - } - s.GeoDistanceSort[key] = append(s.GeoDistanceSort[key], o) - default: - o := []GeoLocation{} - err := json.NewDecoder(bytes.NewReader(value)).Decode(&o) - if err != nil { - return fmt.Errorf("%s | %w", "GeoDistanceSort", err) - } - s.GeoDistanceSort[key] = o - } - } - case "ignore_unmapped": var tmp interface{} dec.Decode(&tmp) @@ -122,6 +97,27 @@ func (s *GeoDistanceSort) UnmarshalJSON(data []byte) error { default: + rawMsg := make(map[string]json.RawMessage, 0) + dec.Decode(&rawMsg) + for key, value := range rawMsg { + switch { + case bytes.HasPrefix(value, []byte("\"")), bytes.HasPrefix(value, []byte("{")): + o := new(GeoLocation) + err := json.NewDecoder(bytes.NewReader(value)).Decode(&o) + if err != nil { + return fmt.Errorf("%s | %w", "GeoDistanceSort", err) + } + s.GeoDistanceSort[key] = append(s.GeoDistanceSort[key], o) + default: + o := []GeoLocation{} + err := json.NewDecoder(bytes.NewReader(value)).Decode(&o) + if err != nil { + return fmt.Errorf("%s | %w", "GeoDistanceSort", err) + } + s.GeoDistanceSort[key] = o + } + } + } } return nil diff --git a/typedapi/types/geopolygonquery.go b/typedapi/types/geopolygonquery.go index 05fa1e740e..a5ed32a735 100644 --- a/typedapi/types/geopolygonquery.go +++ b/typedapi/types/geopolygonquery.go @@ -41,7 +41,7 @@ type GeoPolygonQuery struct { // A boost value between 0 and 1.0 decreases the relevance score. // A value greater than 1.0 increases the relevance score. Boost *float32 `json:"boost,omitempty"` - GeoPolygonQuery map[string]GeoPolygonPoints `json:"GeoPolygonQuery,omitempty"` + GeoPolygonQuery map[string]GeoPolygonPoints `json:"-"` IgnoreUnmapped *bool `json:"ignore_unmapped,omitempty"` QueryName_ *string `json:"_name,omitempty"` ValidationMethod *geovalidationmethod.GeoValidationMethod `json:"validation_method,omitempty"` @@ -78,14 +78,6 @@ func (s *GeoPolygonQuery) UnmarshalJSON(data []byte) error { s.Boost = &f } - case "GeoPolygonQuery": - if s.GeoPolygonQuery == nil { - s.GeoPolygonQuery = make(map[string]GeoPolygonPoints, 0) - } - if err := dec.Decode(&s.GeoPolygonQuery); err != nil { - return fmt.Errorf("%s | %w", "GeoPolygonQuery", err) - } - case "ignore_unmapped": var tmp interface{} dec.Decode(&tmp) @@ -119,6 +111,17 @@ func (s *GeoPolygonQuery) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.GeoPolygonQuery == nil { + s.GeoPolygonQuery = make(map[string]GeoPolygonPoints, 0) + } + raw := NewGeoPolygonPoints() + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "GeoPolygonQuery", err) + } + s.GeoPolygonQuery[key] = *raw + } + } } return nil diff --git a/typedapi/types/geoshapequery.go b/typedapi/types/geoshapequery.go index ac51a51732..12e89370a4 100644 --- a/typedapi/types/geoshapequery.go +++ b/typedapi/types/geoshapequery.go @@ -39,7 +39,7 @@ type GeoShapeQuery struct { // A boost value between 0 and 1.0 decreases the relevance score. // A value greater than 1.0 increases the relevance score. Boost *float32 `json:"boost,omitempty"` - GeoShapeQuery map[string]GeoShapeFieldQuery `json:"GeoShapeQuery,omitempty"` + GeoShapeQuery map[string]GeoShapeFieldQuery `json:"-"` // IgnoreUnmapped Set to `true` to ignore an unmapped field and not match any documents for // this query. // Set to `false` to throw an exception if the field is not mapped. @@ -78,14 +78,6 @@ func (s *GeoShapeQuery) UnmarshalJSON(data []byte) error { s.Boost = &f } - case "GeoShapeQuery": - if s.GeoShapeQuery == nil { - s.GeoShapeQuery = make(map[string]GeoShapeFieldQuery, 0) - } - if err := dec.Decode(&s.GeoShapeQuery); err != nil { - return fmt.Errorf("%s | %w", "GeoShapeQuery", err) - } - case "ignore_unmapped": var tmp interface{} dec.Decode(&tmp) @@ -114,6 +106,17 @@ func (s *GeoShapeQuery) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.GeoShapeQuery == nil { + s.GeoShapeQuery = make(map[string]GeoShapeFieldQuery, 0) + } + raw := NewGeoShapeFieldQuery() + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "GeoShapeQuery", err) + } + s.GeoShapeQuery[key] = *raw + } + } } return nil diff --git a/typedapi/types/numericdecayfunction.go b/typedapi/types/numericdecayfunction.go index 56e2b62333..d2be0947fa 100644 --- a/typedapi/types/numericdecayfunction.go +++ b/typedapi/types/numericdecayfunction.go @@ -34,7 +34,7 @@ type NumericDecayFunction struct { // MultiValueMode Determines how the distance is calculated when a field used for computing the // decay contains multiple values. MultiValueMode *multivaluemode.MultiValueMode `json:"multi_value_mode,omitempty"` - NumericDecayFunction map[string]DecayPlacementdoubledouble `json:"NumericDecayFunction,omitempty"` + NumericDecayFunction map[string]DecayPlacementdoubledouble `json:"-"` } // MarhsalJSON overrides marshalling for types with additional properties diff --git a/typedapi/types/shapequery.go b/typedapi/types/shapequery.go index 9b2b0235ea..9856c787e8 100644 --- a/typedapi/types/shapequery.go +++ b/typedapi/types/shapequery.go @@ -43,7 +43,7 @@ type ShapeQuery struct { // documents. IgnoreUnmapped *bool `json:"ignore_unmapped,omitempty"` QueryName_ *string `json:"_name,omitempty"` - ShapeQuery map[string]ShapeFieldQuery `json:"ShapeQuery,omitempty"` + ShapeQuery map[string]ShapeFieldQuery `json:"-"` } func (s *ShapeQuery) UnmarshalJSON(data []byte) error { @@ -103,16 +103,19 @@ func (s *ShapeQuery) UnmarshalJSON(data []byte) error { } s.QueryName_ = &o - case "ShapeQuery": - if s.ShapeQuery == nil { - s.ShapeQuery = make(map[string]ShapeFieldQuery, 0) - } - if err := dec.Decode(&s.ShapeQuery); err != nil { - return fmt.Errorf("%s | %w", "ShapeQuery", err) - } - default: + if key, ok := t.(string); ok { + if s.ShapeQuery == nil { + s.ShapeQuery = make(map[string]ShapeFieldQuery, 0) + } + raw := NewShapeFieldQuery() + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "ShapeQuery", err) + } + s.ShapeQuery[key] = *raw + } + } } return nil diff --git a/typedapi/types/shardstore.go b/typedapi/types/shardstore.go index a418f4245c..2df0634473 100644 --- a/typedapi/types/shardstore.go +++ b/typedapi/types/shardstore.go @@ -36,7 +36,7 @@ import ( type ShardStore struct { Allocation shardstoreallocation.ShardStoreAllocation `json:"allocation"` AllocationId *string `json:"allocation_id,omitempty"` - ShardStore map[string]ShardStoreNode `json:"ShardStore,omitempty"` + ShardStore map[string]ShardStoreNode `json:"-"` StoreException *ShardStoreException `json:"store_exception,omitempty"` } @@ -65,14 +65,6 @@ func (s *ShardStore) UnmarshalJSON(data []byte) error { return fmt.Errorf("%s | %w", "AllocationId", err) } - case "ShardStore": - if s.ShardStore == nil { - s.ShardStore = make(map[string]ShardStoreNode, 0) - } - if err := dec.Decode(&s.ShardStore); err != nil { - return fmt.Errorf("%s | %w", "ShardStore", err) - } - case "store_exception": if err := dec.Decode(&s.StoreException); err != nil { return fmt.Errorf("%s | %w", "StoreException", err) @@ -80,6 +72,17 @@ func (s *ShardStore) UnmarshalJSON(data []byte) error { default: + if key, ok := t.(string); ok { + if s.ShardStore == nil { + s.ShardStore = make(map[string]ShardStoreNode, 0) + } + raw := NewShardStoreNode() + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "ShardStore", err) + } + s.ShardStore[key] = *raw + } + } } return nil diff --git a/typedapi/types/sortoptions.go b/typedapi/types/sortoptions.go index b5fcae7af8..de6a5dd6c0 100644 --- a/typedapi/types/sortoptions.go +++ b/typedapi/types/sortoptions.go @@ -33,7 +33,7 @@ type SortOptions struct { GeoDistance_ *GeoDistanceSort `json:"_geo_distance,omitempty"` Score_ *ScoreSort `json:"_score,omitempty"` Script_ *ScriptSort `json:"_script,omitempty"` - SortOptions map[string]FieldSort `json:"SortOptions,omitempty"` + SortOptions map[string]FieldSort `json:"-"` } // MarhsalJSON overrides marshalling for types with additional properties diff --git a/typedapi/types/termsquery.go b/typedapi/types/termsquery.go index b7eb6a20e3..95b25e5b8a 100644 --- a/typedapi/types/termsquery.go +++ b/typedapi/types/termsquery.go @@ -40,7 +40,7 @@ type TermsQuery struct { // A value greater than 1.0 increases the relevance score. Boost *float32 `json:"boost,omitempty"` QueryName_ *string `json:"_name,omitempty"` - TermsQuery map[string]TermsQueryField `json:"TermsQuery,omitempty"` + TermsQuery map[string]TermsQueryField `json:"-"` } func (s *TermsQuery) UnmarshalJSON(data []byte) error { @@ -86,16 +86,19 @@ func (s *TermsQuery) UnmarshalJSON(data []byte) error { } s.QueryName_ = &o - case "TermsQuery": - if s.TermsQuery == nil { - s.TermsQuery = make(map[string]TermsQueryField, 0) - } - if err := dec.Decode(&s.TermsQuery); err != nil { - return fmt.Errorf("%s | %w", "TermsQuery", err) - } - default: + if key, ok := t.(string); ok { + if s.TermsQuery == nil { + s.TermsQuery = make(map[string]TermsQueryField, 0) + } + raw := new(TermsQueryField) + if err := dec.Decode(&raw); err != nil { + return fmt.Errorf("%s | %w", "TermsQuery", err) + } + s.TermsQuery[key] = *raw + } + } } return nil