fields.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package ast
  2. // FieldType is an enumeration of specific types relevant to lexing and
  3. // parsing a filter.
  4. type FieldType int
  5. const (
  6. FieldTypeDefault FieldType = iota
  7. FieldTypeSlice
  8. FieldTypeMap
  9. FieldTypeAlias
  10. )
  11. // Field is a Lexer input which acts as a mapping of identifiers used to lex/parse filters.
  12. type Field struct {
  13. // Name contains the name of the specific field as it appears in language.
  14. Name string
  15. fieldType FieldType
  16. }
  17. // IsSlice returns true if the field is a slice. This instructs the lexer that the field
  18. // should allow contains operations.
  19. func (f *Field) IsSlice() bool {
  20. return f.fieldType == FieldTypeSlice
  21. }
  22. // IsMap returns true if the field is a map. This instructs the lexer that the field should
  23. // allow keyed-access operations.
  24. func (f *Field) IsMap() bool {
  25. return f.fieldType == FieldTypeMap
  26. }
  27. // IsAlias returns true if the field is an alias type. This instructs the lexer that the field
  28. // is an alias for custom logical resolution by an external compiler.
  29. func (f *Field) IsAlias() bool {
  30. return f.fieldType == FieldTypeAlias
  31. }
  32. // NewField creates a default string field using the provided name.
  33. func NewField[T ~string](name T) *Field {
  34. return &Field{
  35. Name: string(name),
  36. fieldType: FieldTypeDefault,
  37. }
  38. }
  39. // NewSliceField creates a slice field using the provided name.
  40. func NewSliceField[T ~string](name T) *Field {
  41. return &Field{
  42. Name: string(name),
  43. fieldType: FieldTypeSlice,
  44. }
  45. }
  46. // NewMapField creates a new map field using the provided name.
  47. func NewMapField[T ~string](name T) *Field {
  48. return &Field{
  49. Name: string(name),
  50. fieldType: FieldTypeMap,
  51. }
  52. }
  53. // NewAliasField creates a new alias field using the provided name.
  54. func NewAliasField[T ~string](name T) *Field {
  55. return &Field{
  56. Name: string(name),
  57. fieldType: FieldTypeAlias,
  58. }
  59. }