Search code examples
goanalysis

Why is this `*invalid type` instead of `*Games`?


If you run the analysis package here: https://github.com/frederikhors/iss-goland-invalid-type with:

go run ./analysis

it will print:

field.Name: id - field.Type: string
field.Name: name - field.Type: string        
field.Name: games - field.Type: *invalid type

What I don't understand is why I'm having *invalid type instead of *Games?

Code

  • analyis/main.go:
package main

import (
    "go/types"

    "golang.org/x/tools/go/packages"
)

func main() {
    playerModel := LoadPackage("./player.go")

    var playerStruct *types.Struct

    for _, entity := range playerModel.Types.Scope().Names() {
        if entity == "Player" {

            playerStruct = GetStruct(entity, playerModel)

            break
        }
    }

    for i := 0; i < playerStruct.NumFields(); i++ {
        field := playerStruct.Field(i)

        println("field.Name: " + field.Name() + " - field.Type: " + field.Type().String())
    }
}

func LoadPackage(path string) *packages.Package {
    const mode = packages.NeedTypes |
        packages.NeedName |
        packages.NeedSyntax |
        packages.NeedFiles |
        packages.NeedTypesInfo |
        packages.NeedTypesInfo |
        packages.NeedModule

    cfg := &packages.Config{Mode: mode}

    pkgs, err := packages.Load(cfg, path)
    if err != nil {
        panic(err)
    }

    return pkgs[0]
}

func GetStruct(structName string, pkg *packages.Package) *types.Struct {
    foundStruct := pkg.Types.Scope().Lookup(structName)

    if foundStruct == nil {
        return nil
    }

    res, _ := foundStruct.Type().Underlying().(*types.Struct)

    return res
}
  • player.go:
type Player struct {
    id    string
    name  string
    games *Games
}
  • games.go:
package main

type Games struct {
    wins   []string
    losses []string
}

Solution

  • You are explicitly loading only a single file with LoadPackage("./player.go"). And that file is not the one in which the Games type is declared. To load information on all types of a package you need to load the whole package.

    You need to use LoadPackage(".").