fileActions is required because we cannot get the variable name of the enum at runtime. So parse the package to get it. This is purely to ensure enum names are consistent, which is a bit annoying, but not too bad.
(file *ast.File)
| 153 | // at runtime. So parse the package to get it. This is purely to ensure enum |
| 154 | // names are consistent, which is a bit annoying, but not too bad. |
| 155 | func fileActions(file *ast.File) map[string]string { |
| 156 | // actions is a map from the enum value -> enum name |
| 157 | actions := make(map[string]string) |
| 158 | |
| 159 | // Find the action consts |
| 160 | fileDeclLoop: |
| 161 | for _, decl := range file.Decls { |
| 162 | switch typedDecl := decl.(type) { |
| 163 | case *ast.GenDecl: |
| 164 | if len(typedDecl.Specs) == 0 { |
| 165 | continue |
| 166 | } |
| 167 | // This is the right on, loop over all idents, pull the actions |
| 168 | for _, spec := range typedDecl.Specs { |
| 169 | vSpec, ok := spec.(*ast.ValueSpec) |
| 170 | if !ok { |
| 171 | continue fileDeclLoop |
| 172 | } |
| 173 | |
| 174 | typeIdent, ok := vSpec.Type.(*ast.Ident) |
| 175 | if !ok { |
| 176 | continue fileDeclLoop |
| 177 | } |
| 178 | |
| 179 | if typeIdent.Name != "Action" || len(vSpec.Values) != 1 || len(vSpec.Names) != 1 { |
| 180 | continue fileDeclLoop |
| 181 | } |
| 182 | |
| 183 | literal, ok := vSpec.Values[0].(*ast.BasicLit) |
| 184 | if !ok { |
| 185 | continue fileDeclLoop |
| 186 | } |
| 187 | actions[strings.Trim(literal.Value, `"`)] = vSpec.Names[0].Name |
| 188 | } |
| 189 | default: |
| 190 | continue |
| 191 | } |
| 192 | } |
| 193 | return actions |
| 194 | } |
| 195 | |
| 196 | type ActionDetails struct { |
| 197 | Enum string |
no outgoing calls
no test coverage detected