collectPackageConsts collects exported string constants from a file into the global packageDeclarations map, keyed by package name.
(file *ast.File)
| 230 | // collectPackageConsts collects exported string constants from a file into |
| 231 | // the global packageDeclarations map, keyed by package name. |
| 232 | func collectPackageConsts(file *ast.File) { |
| 233 | pkgName := file.Name.Name |
| 234 | |
| 235 | if packageDeclarations[pkgName] == nil { |
| 236 | packageDeclarations[pkgName] = make(map[string]string) |
| 237 | } |
| 238 | |
| 239 | for _, decl := range file.Decls { |
| 240 | genDecl, ok := decl.(*ast.GenDecl) |
| 241 | if !ok || genDecl.Tok != token.CONST { |
| 242 | continue |
| 243 | } |
| 244 | |
| 245 | for _, spec := range genDecl.Specs { |
| 246 | valueSpec, ok := spec.(*ast.ValueSpec) |
| 247 | if !ok { |
| 248 | continue |
| 249 | } |
| 250 | |
| 251 | for i, name := range valueSpec.Names { |
| 252 | if !ast.IsExported(name.Name) { |
| 253 | continue |
| 254 | } |
| 255 | |
| 256 | if i >= len(valueSpec.Values) { |
| 257 | continue |
| 258 | } |
| 259 | |
| 260 | if lit, ok := valueSpec.Values[i].(*ast.BasicLit); ok { |
| 261 | if lit.Kind == token.STRING { |
| 262 | packageDeclarations[pkgName][name.Name] = strings.Trim(lit.Value, `"`) |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // resolveStringExpr attempts to resolve an expression to a string value. |
| 271 | // Examples: |