cql

package
v1.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cql parses and validates EPO OPS Contextual CQLQuery Language (CQL) search queries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFieldDescription

func GetFieldDescription(field string) string

GetFieldDescription returns the description of a CQL field. Returns empty string if the field is not valid.

func GetValidFields

func GetValidFields() []string

GetValidFields returns a slice of all valid field names.

func IsValidField

func IsValidField(field string) bool

IsValidField checks if a field name is valid in EPO CQL.

func IsValidOperator

func IsValidOperator(op string) bool

IsValidOperator checks if an operator is valid in EPO CQL.

Types

type CQLQuery

type CQLQuery struct {
	// Raw is the original query string
	Raw string

	// Tokens is the parsed token stream
	Tokens []CQLToken

	// Valid indicates whether the query passed validation
	Valid bool

	// Errors contains any validation errors as plain strings.
	// Retained for backward compatibility; new code should consult
	// Failures instead which preserves the structured failure kind.
	Errors []string

	// Failures lists structured validation failures from the parser.
	// One entry per Errors entry; same order.
	Failures []*CQLValidationFailure
}

CQLQuery represents a parsed CQL query.

func ParseCQL

func ParseCQL(query string) (*CQLQuery, error)

ParseCQL parses a CQL query string and returns a CQLQuery object.

Example queries:

  • "ti=bluetooth"
  • "ti=bluetooth AND pa=ericsson"
  • "(ti=5g OR ab=5g) AND pd>=20200101"
  • "pa=\"Apple Inc\" AND ic=H04W"

Returns:

  • *CQLQuery: The parsed query with tokens and validation status
  • error: An error if the query is completely invalid or empty

func (*CQLQuery) GetFields

func (q *CQLQuery) GetFields() []string

GetFields returns all unique fields used in the query.

func (*CQLQuery) HasField

func (q *CQLQuery) HasField(field string) bool

HasField checks if the query contains a specific field.

func (*CQLQuery) String

func (q *CQLQuery) String() string

String returns the raw query string.

func (*CQLQuery) TokenCount

func (q *CQLQuery) TokenCount() int

TokenCount returns the number of tokens in the parsed query.

func (*CQLQuery) URLEncode

func (q *CQLQuery) URLEncode() string

URLEncode returns the CQL query properly URL-encoded for use in API requests.

func (*CQLQuery) Validate

func (q *CQLQuery) Validate() error

Validate checks if the CQL query is valid. Returns *CQLValidationError when one or more checks failed; callers can errors.As into it to dispatch on the specific failure Kind. The error string format is unchanged from earlier versions.

type CQLToken

type CQLToken struct {
	Type  TokenType
	Value string
	Pos   int // Position in original string
}

CQLToken represents a token in a CQL query.

type CQLValidationError added in v1.2.0

type CQLValidationError struct {
	Failures []*CQLValidationFailure
}

CQLValidationError is the error type returned by CQLQuery.Validate when one or more validation checks failed. Callers can errors.As into this type and inspect Failures to dispatch on the specific Kind(s).

func (*CQLValidationError) Error added in v1.2.0

func (e *CQLValidationError) Error() string

func (*CQLValidationError) First added in v1.2.0

First returns the first validation failure, or nil if there are none. Useful for picking a single dominant failure kind when classifying.

type CQLValidationFailure added in v1.2.0

type CQLValidationFailure struct {
	Kind    CQLValidationKind
	Field   string // offending field name when Kind == CQLValidationUnknownField
	Pos     int    // position in the raw query, when available; 0 otherwise
	Message string // human-readable description, used as Error()
}

CQLValidationFailure describes a single CQL validation problem.

func (*CQLValidationFailure) Error added in v1.2.0

func (f *CQLValidationFailure) Error() string

type CQLValidationKind added in v1.2.0

type CQLValidationKind string

CQLValidationKind classifies a single CQL validation failure so callers can route on the failure mode without inspecting prose. Stable across minor releases; new constants may be added but existing values do not change meaning.

const (
	// CQLValidationUnknownField - the query referenced a field name that
	// is not in the EPO field list (ti, ab, pa, in, pn, ic, cpc, pd).
	CQLValidationUnknownField CQLValidationKind = "unknown_field"
	// CQLValidationUnclosedParens - more '(' than ')' in the query.
	CQLValidationUnclosedParens CQLValidationKind = "unclosed_parens"
	// CQLValidationUnmatchedParens - a ')' appeared without a matching '('.
	CQLValidationUnmatchedParens CQLValidationKind = "unmatched_parens"
	// CQLValidationEmptyQuery - the query tokenized to nothing.
	CQLValidationEmptyQuery CQLValidationKind = "empty_query"
	// CQLValidationNoFieldPattern - tokens present but no field=value
	// pattern detected. Often a sign the caller used another provider's
	// grammar (e.g. USPTO field:value syntax) on the EPO endpoint.
	CQLValidationNoFieldPattern CQLValidationKind = "no_field_pattern"
)

type TokenType

type TokenType int

TokenType represents the type of a CQL token.

const (
	TokenField TokenType = iota
	TokenOperator
	TokenValue
	TokenEquals
	TokenLParen
	TokenRParen
	TokenQuote
	TokenWhitespace
	TokenUnknown
)

CQLToken type constants enumerate the kinds of tokens a CQL query can contain.

func (TokenType) String

func (t TokenType) String() string

String returns a string representation of a token type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL