ast.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package ast
  2. type (
  3. // ParamType holds the necessary information about a parameter type for the parser to lookup for.
  4. ParamType interface {
  5. // The name of the parameter type.
  6. // Indent should contain the characters for the parser.
  7. Indent() string
  8. }
  9. // MasterParamType if implemented and its `Master()` returns true then empty type param will be translated to this param type.
  10. // Also its functions will be available to the rest of the macro param type's funcs.
  11. //
  12. // Only one Master is allowed.
  13. MasterParamType interface {
  14. ParamType
  15. Master() bool
  16. }
  17. // TrailingParamType if implemented and its `Trailing()` returns true
  18. // then it should be declared at the end of a route path and can accept any trailing path segment as one parameter.
  19. TrailingParamType interface {
  20. ParamType
  21. Trailing() bool
  22. }
  23. // AliasParamType if implemeneted nad its `Alias()` returns a non-empty string
  24. // then the param type can be written with that string literal too.
  25. AliasParamType interface {
  26. ParamType
  27. Alias() string
  28. }
  29. )
  30. // IsMaster returns true if the "pt" param type is a master one.
  31. func IsMaster(pt ParamType) bool {
  32. p, ok := pt.(MasterParamType)
  33. return ok && p.Master()
  34. }
  35. // IsTrailing returns true if the "pt" param type is a marked as trailing,
  36. // which should accept more than one path segment when in the end.
  37. func IsTrailing(pt ParamType) bool {
  38. p, ok := pt.(TrailingParamType)
  39. return ok && p.Trailing()
  40. }
  41. // HasAlias returns any alias of the "pt" param type.
  42. // If alias is empty or not found then it returns false as its second output argument.
  43. func HasAlias(pt ParamType) (string, bool) {
  44. if p, ok := pt.(AliasParamType); ok {
  45. alias := p.Alias()
  46. return alias, len(alias) > 0
  47. }
  48. return "", false
  49. }
  50. // GetMasterParamType accepts a list of ParamType and returns its master.
  51. // If no `Master` specified:
  52. // and len(paramTypes) > 0 then it will return the first one,
  53. // otherwise it returns nil.
  54. func GetMasterParamType(paramTypes ...ParamType) ParamType {
  55. for _, pt := range paramTypes {
  56. if IsMaster(pt) {
  57. return pt
  58. }
  59. }
  60. if len(paramTypes) > 0 {
  61. return paramTypes[0]
  62. }
  63. return nil
  64. }
  65. // LookupParamType accepts the string
  66. // representation of a parameter type.
  67. // Example:
  68. // "string"
  69. // "number" or "int"
  70. // "long" or "int64"
  71. // "uint8"
  72. // "uint64"
  73. // "boolean" or "bool"
  74. // "alphabetical"
  75. // "file"
  76. // "path"
  77. func LookupParamType(indentOrAlias string, paramTypes ...ParamType) (ParamType, bool) {
  78. for _, pt := range paramTypes {
  79. if pt.Indent() == indentOrAlias {
  80. return pt, true
  81. }
  82. if alias, has := HasAlias(pt); has {
  83. if alias == indentOrAlias {
  84. return pt, true
  85. }
  86. }
  87. }
  88. return nil, false
  89. }
  90. // ParamStatement is a struct
  91. // which holds all the necessary information about a macro parameter.
  92. // It holds its type (string, int, alphabetical, file, path),
  93. // its source ({param:type}),
  94. // its name ("param"),
  95. // its attached functions by the user (min, max...)
  96. // and the http error code if that parameter
  97. // failed to be evaluated.
  98. type ParamStatement struct {
  99. Src string // the original unparsed source, i.e: {id:int range(1,5) else 404}
  100. Name string // id
  101. Type ParamType // int
  102. Funcs []ParamFunc // range
  103. ErrorCode int // 404
  104. }
  105. // ParamFunc holds the name of a parameter's function
  106. // and its arguments (values)
  107. // A param func is declared with:
  108. // {param:int range(1,5)},
  109. // the range is the
  110. // param function name
  111. // the 1 and 5 are the two param function arguments
  112. // range(1,5)
  113. type ParamFunc struct {
  114. Name string // range
  115. Args []string // ["1","5"]
  116. }