Hi,

First thing first, 
really sorry for the missing code coloration,
but the ML UI is buggy here, 
i can t post when i use the code highlight function :x

I m facing a difficulty with the type checker 
when i try to identify the package path
of a selector expression.

The code i parse look likes this,

package y

import "fmt"
var x = map[string]interface{
"key": fmt.Sprintf,
}

I want to identify the import package path of 
"fmt.Sprintf" selector expression.

So i loaded the source code via the loader

// GetProgram creates a new Program of a list of packages.
func GetProgram(pkgs []string) (*loader.Program, error) {

    var conf loader.Config
    _, err := conf.FromArgs(pkgs, false)
    if err != nil {
        return nil, err
    }

    prog, err2 := conf.Load()
    if err2 != nil {
        return nil, err2
    }

    return prog, err
}

I identified and got the KeyValues,
now for each value, i test case its type,
if its a SelExpr, i try to get the package path import
of SelExpr.X.


keyValues := id.Obj.Decl.(*ast.ValueSpec).Values[0].(*ast.CompositeLit).Elts
for _, e := range keyValues {
expr := e.(*ast.KeyValueExpr).Value
key := e.(*ast.KeyValueExpr).Key.(*ast.BasicLit)
switch node := expr.(type) {
case *ast.Ident:
    if ast.IsExported(node.Name) {
        ret = append(ret, PublicIdent{
            FuncName: key.Value,
            Sel:      ourpkg.Pkg.Path() + "." + node.Name,
            Pkg:      ourpkg.Pkg.Path(),
        })
    }
case *ast.SelectorExpr:
    foreignPkg := ourpkg.Uses[node.X.(*ast.Ident)].(*types.PkgName) // this 
is wrong, returns "y"

// problem is here, its nil :x
    fmt.Println(ourpkg.Selections[node])

// problem is here

    ret = append(ret, PublicIdent{
        FuncName: key.Value,
        Sel:      node.X.(*ast.Ident).Name + "." + node.Sel.Name,
        Pkg:      foreignPkg.Pkg().Path(), // this is wrong
, returns "y"                            })
default:
    panic(
        fmt.Errorf("export.XXX: unhandled ast node type %v\n%#v",
            node, node),
    )
}


I tried to ask types.Info.Defs/Implicits, 
but none of them give me a kind of Package instance
from which i can get the correct import path.

types.Info.Uses[sel] returns the "y" package,
so its not the info i m looking for.

Do you have an idea to solve that ?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to