Files
opencloud/pkg/ast/ast.go
T
Dominik Schmidt c6cc01750a fix(kql): normalise geo.bbox latitude order for backend-consistent behaviour
Sorts the two latitudes in the KQL layer so MinLat <= MaxLat. Only bleve
strictly requires it (it errors on an inverted box; OpenSearch tolerates it),
but doing it centrally keeps behaviour consistent across backends and the AST
easy to reason about. Longitude order is preserved so an antimeridian-crossing
box (minLon > maxLon) still works, which both backends handle identically.
2026-12-04 11:01:26 +01:00

160 lines
3.1 KiB
Go

// Package ast provides available ast nodes.
package ast
import (
"time"
)
// Node represents abstract syntax tree node
type Node interface {
Location() *Location
}
// Position represents a specific location in the source
type Position struct {
Line int
Column int
}
// Location represents the location of a node in the AST
type Location struct {
Start Position `json:"start"`
End Position `json:"end"`
Source *string `json:"source,omitempty"`
}
// Base contains shared node attributes
// each node should inherit from this
type Base struct {
Loc *Location
}
// Location is the source location of the Node
func (b *Base) Location() *Location { return b.Loc }
// Ast represents the query - node structure as abstract syntax tree
type Ast struct {
*Base
Nodes []Node `json:"body"`
}
// StringNode represents a string value
type StringNode struct {
*Base
Key string
Value string
Exact bool
// CaseInsensitive marks a case-insensitive restriction; set by the search
// lowering pass, a backend routes it to the field's lowercased form.
CaseInsensitive bool
}
// BooleanNode represents a bool value
type BooleanNode struct {
*Base
Key string
Value bool
}
// DateTimeNode represents a time.Time value
type DateTimeNode struct {
*Base
Key string
Operator *OperatorNode
Value time.Time
}
// NumberNode represents a numeric value
type NumberNode struct {
*Base
Key string
Operator *OperatorNode
Value float64
}
// OperatorNode represents an operator value like
// AND, OR, NOT, =, <= ... and so on
type OperatorNode struct {
*Base
Value string
}
// GroupNode represents a collection of many grouped nodes
type GroupNode struct {
*Base
Key string
Nodes []Node
}
// GeoPoint is a lat/lon vertex.
type GeoPoint struct {
Lat float64
Lon float64
}
// GeoDistanceNode represents a geo.distance(lat, lon, radius) predicate; Radius is in meters.
type GeoDistanceNode struct {
*Base
Key string
Lat float64
Lon float64
Radius float64
}
// GeoBoundingBoxNode represents a geo.bbox(minLat, minLon, maxLat, maxLon)
// predicate. Latitude is normalised so MinLat <= MaxLat; longitude order is
// preserved, so MinLon > MaxLon denotes a box crossing the antimeridian.
type GeoBoundingBoxNode struct {
*Base
Key string
MinLat float64
MinLon float64
MaxLat float64
MaxLon float64
}
// GeoPolygonNode represents a geo.polygon(lat lon, ...) predicate.
type GeoPolygonNode struct {
*Base
Key string
Points []GeoPoint
}
// NodeKey tries to return the node key
func NodeKey(n Node) string {
switch node := n.(type) {
case *StringNode:
return node.Key
case *DateTimeNode:
return node.Key
case *BooleanNode:
return node.Key
case *GroupNode:
return node.Key
case *GeoDistanceNode:
return node.Key
case *GeoBoundingBoxNode:
return node.Key
case *GeoPolygonNode:
return node.Key
default:
return ""
}
}
// NodeValue tries to return the node key
func NodeValue(n Node) any {
switch node := n.(type) {
case *StringNode:
return node.Value
case *DateTimeNode:
return node.Value
case *BooleanNode:
return node.Value
case *GroupNode:
return node.Nodes
default:
return ""
}
}