fields.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Field equivalence is determined by name and type.
  18. func (f *Field) Equal(other *Field) bool {
  19. if f == nil || other == nil {
  20. return false
  21. }
  22. return f.Name == other.Name && f.fieldType == other.fieldType
  23. }
  24. // IsSlice returns true if the field is a slice. This instructs the lexer that the field
  25. // should allow contains operations.
  26. func (f *Field) IsSlice() bool {
  27. return f.fieldType == FieldTypeSlice
  28. }
  29. // IsMap returns true if the field is a map. This instructs the lexer that the field should
  30. // allow keyed-access operations.
  31. func (f *Field) IsMap() bool {
  32. return f.fieldType == FieldTypeMap
  33. }
  34. // IsAlias returns true if the field is an alias type. This instructs the lexer that the field
  35. // is an alias for custom logical resolution by an external compiler.
  36. func (f *Field) IsAlias() bool {
  37. return f.fieldType == FieldTypeAlias
  38. }
  39. // NewField creates a default string field using the provided name.
  40. func NewField[T ~string](name T) *Field {
  41. return &Field{
  42. Name: string(name),
  43. fieldType: FieldTypeDefault,
  44. }
  45. }
  46. // NewSliceField creates a slice field using the provided name.
  47. func NewSliceField[T ~string](name T) *Field {
  48. return &Field{
  49. Name: string(name),
  50. fieldType: FieldTypeSlice,
  51. }
  52. }
  53. // NewMapField creates a new map field using the provided name.
  54. func NewMapField[T ~string](name T) *Field {
  55. return &Field{
  56. Name: string(name),
  57. fieldType: FieldTypeMap,
  58. }
  59. }
  60. // NewAliasField creates a new alias field using the provided name.
  61. func NewAliasField[T ~string](name T) *Field {
  62. return &Field{
  63. Name: string(name),
  64. fieldType: FieldTypeAlias,
  65. }
  66. }