I solved it by experimenting.

*ast.Ident for the "string"
*ast.SelectExpr for the package type

Seems like there is no easier way.

On Tuesday, April 17, 2018 at 7:55:13 PM UTC+2, Darko Luketic wrote:
>
> Hi,
>
> I'm trying to parse a file (or rather more files, but let's keep it 
> simple).
>
> package models
>
> import "github.com/globalsign/mgo/bson"
>
> type Community struct {
>    Id           bson.ObjectId `bson:"_id,omitempty" json:"id"`
>    OwnerId      string        `bson:"owner_id" json:"owner_id"`
>    CategorySlug string        `bson:"category_slug" json:"category_slug"`
>    Name         string        `bson:"name" json:"name"`
>    Description  string        `bson:"description" json:"description"`
>    Subdomain    string        `bson:"subdomain" json:"subdomain"`
>    Domain       string        `bson:"domain" json:"domain"`
> }
>
>
> and I essentiall do
>
> ast.Inspect(v, func(node ast.Node) bool {
>    switch n := node.(type) {
>    case *ast.TypeSpec:
>       m.Name = n.Name.String()
>    case *ast.Field:
>       fmt.Println("******************0")
>       fmt.Println("IS_FIELD", n, "END")
>       fmt.Println(n.Names)
>       fmt.Printf("%+v\n", n.Type)
>       switch nn := n.Type.(type) {
>       default:
>          fmt.Printf("%+v", nn)
>       }
>       fmt.Println(n.Doc)
>       fmt.Println(n.Comment)
>       fmt.Printf("%+v\n", n.Tag)
>
>       fmt.Println("******************1")
>
>    default:
>       fmt.Println(n, "END")
>    }
>    return true
> })
>
>
>
> but you see, Id is type bson.ObjectId and the output of it is different 
> from it being a string
>
> &{X:bson Sel:ObjectId}
>
> where a string is just a string with "string" content, at least that's 
> what I assume.
>
> What type is "nn" here?
> How do I find that out short of trying all *ast.Types. 
> The doc just says 
>
> Type    Expr <https://golang.org/pkg/go/ast/#Expr>          // 
> field/method/parameter type
> but "Expr" is a generic term with a description of
> "All expression nodes implement the Expr interface."
> https://golang.org/pkg/go/ast/#Expr
>
> Well carefully browsing the documentation here I can somewhat guess from the 
> fields that 
> &{X:bson Sel:ObjectId} is a *ast.SelectorExpr
> https://golang.org/pkg/go/ast/#SelectorExpr
>
> but what is a plain field type?
>
> Is there a surefire method to find that out short of typechecking every 
> possible type?
>
>
>
>

-- 
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