Mist mapping

This commit is contained in:
2026-04-22 09:05:32 +02:00
parent 842383ba6e
commit 10a304bfe4
3 changed files with 484 additions and 13 deletions
+32 -13
View File
@@ -33,15 +33,20 @@ type StructSymbol struct {
} }
type TopLevelSymbolScope struct { type TopLevelSymbolScope struct {
Structs map[string]StructSymbol `json:"structs"` PackageName string `json:"package_name"`
Functions map[string]FunctionSymbol `json:"functions"` Structs map[string]StructSymbol `json:"structs"`
Functions map[string]FunctionSymbol `json:"functions"`
} }
func main() { func main() {
cfg := &packages.Config{ cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo, Mode: packages.NeedName |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedModule,
} }
// you can expand this to multiple packages later
pkgs, err := packages.Load(cfg, "fmt") pkgs, err := packages.Load(cfg, "fmt")
if err != nil { if err != nil {
panic(err) panic(err)
@@ -51,12 +56,16 @@ func main() {
os.Exit(1) os.Exit(1)
} }
result := TopLevelSymbolScope{ // 🔑 FINAL RESULT: map[import_path]scope
Structs: make(map[string]StructSymbol), result := make(map[string]TopLevelSymbolScope)
Functions: make(map[string]FunctionSymbol),
}
for _, pkg := range pkgs { for _, pkg := range pkgs {
scopeData := TopLevelSymbolScope{
PackageName: pkg.Name, // e.g. "http"
Structs: make(map[string]StructSymbol),
Functions: make(map[string]FunctionSymbol),
}
scope := pkg.Types.Scope() scope := pkg.Types.Scope()
for _, name := range scope.Names() { for _, name := range scope.Names() {
@@ -69,15 +78,17 @@ func main() {
switch obj := obj.(type) { switch obj := obj.(type) {
case *types.Func: case *types.Func:
fn := buildFunction(obj) scopeData.Functions[name] = buildFunction(obj)
result.Functions[name] = fn
case *types.TypeName: case *types.TypeName:
if strct, ok := buildStruct(obj); ok { if strct, ok := buildStruct(obj); ok {
result.Structs[name] = strct scopeData.Structs[name] = strct
} }
} }
} }
// 🔑 key = import path (e.g. "net/http")
result[pkg.PkgPath] = scopeData
} }
out, err := json.MarshalIndent(result, "", " ") out, err := json.MarshalIndent(result, "", " ")
@@ -114,7 +125,7 @@ func buildFunction(fn *types.Func) FunctionSymbol {
} }
return FunctionSymbol{ return FunctionSymbol{
Export: token.IsExported(fn.Name()), Export: true, // already filtered before calling
Name: fn.Name(), Name: fn.Name(),
Params: params, Params: params,
ReturnTypes: returnTypes, ReturnTypes: returnTypes,
@@ -134,8 +145,12 @@ func buildStruct(tn *types.TypeName) (StructSymbol, bool) {
for i := 0; i < strct.NumFields(); i++ { for i := 0; i < strct.NumFields(); i++ {
f := strct.Field(i) f := strct.Field(i)
if !token.IsExported(f.Name()) {
continue
}
fields[f.Name()] = VarSymbol{ fields[f.Name()] = VarSymbol{
Export: token.IsExported(f.Name()), Export: true,
Name: f.Name(), Name: f.Name(),
VarType: TypeSymbol(f.Type().String()), VarType: TypeSymbol(f.Type().String()),
} }
@@ -147,11 +162,15 @@ func buildStruct(tn *types.TypeName) (StructSymbol, bool) {
for i := 0; i < methodSet.Len(); i++ { for i := 0; i < methodSet.Len(); i++ {
m := methodSet.At(i).Obj().(*types.Func) m := methodSet.At(i).Obj().(*types.Func)
if !token.IsExported(m.Name()) {
continue
}
methods[m.Name()] = buildFunction(m) methods[m.Name()] = buildFunction(m)
} }
return StructSymbol{ return StructSymbol{
Export: token.IsExported(tn.Name()), Export: true,
Name: tn.Name(), Name: tn.Name(),
Fields: fields, Fields: fields,
Methods: methods, Methods: methods,
+450
View File
@@ -0,0 +1,450 @@
{
"fmt": {
"package_name": "fmt",
"structs": {},
"functions": {
"Append": {
"export": true,
"name": "Append",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"b": {
"export": true,
"var_type": "[]byte",
"name": "b"
}
},
"return_types": [
"[]byte"
]
},
"Appendf": {
"export": true,
"name": "Appendf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"b": {
"export": true,
"var_type": "[]byte",
"name": "b"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
}
},
"return_types": [
"[]byte"
]
},
"Appendln": {
"export": true,
"name": "Appendln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"b": {
"export": true,
"var_type": "[]byte",
"name": "b"
}
},
"return_types": [
"[]byte"
]
},
"Errorf": {
"export": true,
"name": "Errorf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
}
},
"return_types": [
"error"
]
},
"FormatString": {
"export": true,
"name": "FormatString",
"params": {
"state": {
"export": true,
"var_type": "fmt.State",
"name": "state"
},
"verb": {
"export": true,
"var_type": "rune",
"name": "verb"
}
},
"return_types": [
"string"
]
},
"Fprint": {
"export": true,
"name": "Fprint",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"w": {
"export": true,
"var_type": "io.Writer",
"name": "w"
}
},
"return_types": [
"int",
"error"
]
},
"Fprintf": {
"export": true,
"name": "Fprintf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
},
"w": {
"export": true,
"var_type": "io.Writer",
"name": "w"
}
},
"return_types": [
"int",
"error"
]
},
"Fprintln": {
"export": true,
"name": "Fprintln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"w": {
"export": true,
"var_type": "io.Writer",
"name": "w"
}
},
"return_types": [
"int",
"error"
]
},
"Fscan": {
"export": true,
"name": "Fscan",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"r": {
"export": true,
"var_type": "io.Reader",
"name": "r"
}
},
"return_types": [
"int",
"error"
]
},
"Fscanf": {
"export": true,
"name": "Fscanf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
},
"r": {
"export": true,
"var_type": "io.Reader",
"name": "r"
}
},
"return_types": [
"int",
"error"
]
},
"Fscanln": {
"export": true,
"name": "Fscanln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"r": {
"export": true,
"var_type": "io.Reader",
"name": "r"
}
},
"return_types": [
"int",
"error"
]
},
"Print": {
"export": true,
"name": "Print",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"int",
"error"
]
},
"Printf": {
"export": true,
"name": "Printf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
}
},
"return_types": [
"int",
"error"
]
},
"Println": {
"export": true,
"name": "Println",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"int",
"error"
]
},
"Scan": {
"export": true,
"name": "Scan",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"int",
"error"
]
},
"Scanf": {
"export": true,
"name": "Scanf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
}
},
"return_types": [
"int",
"error"
]
},
"Scanln": {
"export": true,
"name": "Scanln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"int",
"error"
]
},
"Sprint": {
"export": true,
"name": "Sprint",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"string"
]
},
"Sprintf": {
"export": true,
"name": "Sprintf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
}
},
"return_types": [
"string"
]
},
"Sprintln": {
"export": true,
"name": "Sprintln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
}
},
"return_types": [
"string"
]
},
"Sscan": {
"export": true,
"name": "Sscan",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"str": {
"export": true,
"var_type": "string",
"name": "str"
}
},
"return_types": [
"int",
"error"
]
},
"Sscanf": {
"export": true,
"name": "Sscanf",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"format": {
"export": true,
"var_type": "string",
"name": "format"
},
"str": {
"export": true,
"var_type": "string",
"name": "str"
}
},
"return_types": [
"int",
"error"
]
},
"Sscanln": {
"export": true,
"name": "Sscanln",
"params": {
"a": {
"export": true,
"var_type": "[]any",
"name": "a"
},
"str": {
"export": true,
"var_type": "string",
"name": "str"
}
},
"return_types": [
"int",
"error"
]
}
}
}
}
+2
View File
@@ -1,5 +1,7 @@
package main; package main;
import "fmt";
function main() { function main() {
let a = 10; let a = 10;
} }