vitess.io/vitess@v0.16.2/go/tools/asthelpergen/integration/types.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // nolint 18 package integration 19 20 import ( 21 "fmt" 22 "strings" 23 ) 24 25 //go:generate go run ../main --in . --iface vitess.io/vitess/go/tools/asthelpergen/integration.AST --clone_exclude "*NoCloneType" 26 27 // AST is the interface all interface types implement 28 type AST interface { 29 String() string 30 } 31 32 // Empty struct impl of the iface 33 type Leaf struct { 34 v int 35 } 36 37 func (l *Leaf) String() string { 38 if l == nil { 39 return "nil" 40 } 41 return fmt.Sprintf("Leaf(%d)", l.v) 42 } 43 44 // Container implements the interface ByRef 45 type RefContainer struct { 46 ASTType AST 47 NotASTType int 48 ASTImplementationType *Leaf 49 } 50 51 func (r *RefContainer) String() string { 52 if r == nil { 53 return "nil" 54 } 55 var astType = "" 56 if r.ASTType == nil { 57 astType = "nil" 58 } else { 59 astType = r.ASTType.String() 60 } 61 return fmt.Sprintf("RefContainer{%s, %d, %s}", astType, r.NotASTType, r.ASTImplementationType.String()) 62 } 63 64 // Container implements the interface ByRef 65 type RefSliceContainer struct { 66 ASTElements []AST 67 NotASTElements []int 68 ASTImplementationElements []*Leaf 69 } 70 71 func (r *RefSliceContainer) String() string { 72 return fmt.Sprintf("RefSliceContainer{%s, %s, %s}", sliceStringAST(r.ASTElements...), "r.NotASTType", sliceStringLeaf(r.ASTImplementationElements...)) 73 } 74 75 // Container implements the interface ByValue 76 type ValueContainer struct { 77 ASTType AST 78 NotASTType int 79 ASTImplementationType *Leaf 80 } 81 82 func (r ValueContainer) String() string { 83 return fmt.Sprintf("ValueContainer{%s, %d, %s}", r.ASTType.String(), r.NotASTType, r.ASTImplementationType.String()) 84 } 85 86 // Container implements the interface ByValue 87 type ValueSliceContainer struct { 88 ASTElements []AST 89 NotASTElements []int 90 ASTImplementationElements []*Leaf 91 } 92 93 func (r ValueSliceContainer) String() string { 94 return fmt.Sprintf("ValueSliceContainer{%s, %s, %s}", sliceStringAST(r.ASTElements...), "r.NotASTType", sliceStringLeaf(r.ASTImplementationElements...)) 95 } 96 97 // We need to support these types - a slice of AST elements can implement the interface 98 type InterfaceSlice []AST 99 100 func (r InterfaceSlice) String() string { 101 var elements []string 102 for _, el := range r { 103 elements = append(elements, el.String()) 104 } 105 106 return "[" + strings.Join(elements, ", ") + "]" 107 } 108 109 // We need to support these types - a slice of AST elements can implement the interface 110 type Bytes []byte 111 112 func (r Bytes) String() string { 113 return string(r) 114 } 115 116 type LeafSlice []*Leaf 117 118 func (r LeafSlice) String() string { 119 var elements []string 120 for _, el := range r { 121 elements = append(elements, el.String()) 122 } 123 return strings.Join(elements, ", ") 124 } 125 126 type BasicType int 127 128 func (r BasicType) String() string { 129 return fmt.Sprintf("int(%d)", r) 130 } 131 132 const ( 133 // these consts are here to try to trick the generator 134 thisIsNotAType BasicType = 1 135 thisIsNotAType2 BasicType = 2 136 ) 137 138 // We want to support all types that are used as field types, which can include interfaces. 139 // Example would be sqlparser.Expr that implements sqlparser.SQLNode 140 type SubIface interface { 141 AST 142 iface() 143 } 144 145 type SubImpl struct { 146 inner SubIface 147 field *bool 148 } 149 150 func (r *SubImpl) String() string { 151 return "SubImpl" 152 } 153 func (r *SubImpl) iface() {} 154 155 type InterfaceContainer struct { 156 v any 157 } 158 159 func (r InterfaceContainer) String() string { 160 return fmt.Sprintf("%v", r.v) 161 } 162 163 type NoCloneType struct { 164 v int 165 } 166 167 func (r *NoCloneType) String() string { 168 return fmt.Sprintf("NoClone(%d)", r.v) 169 } 170 171 type Visit func(node AST) (bool, error) 172 173 type application struct { 174 pre, post ApplyFunc 175 cur Cursor 176 } 177 178 var Equals = &Comparator{}