BlueMagma
BlueMagma

Reputation: 2515

Reflect on struct type from reading a .go file

I'm discovering generator (go generate) and I'm trying to generate Validation function for my struct.

The idea is that I don't want my program to use reflect at runtime, I would rather have a generator use reflect to generate the actual method I want to use.

the problem is I can't import my structs in the generator code, the only way I found so far was to read the .go file from the generator and manually parse the types defined there using regex

I've got something like

models/models.go:

package models

//go:generate go run ../generator.go -file models.go

type MyStruct struct {
    ...
}

generator.go:

package main

func main() {
    f, err := ioutil.ReadFile(fileName) // I read filename from the flag provided
    ...
    // I parse f to generate my stuff
}

I would very much prefer to have an introspection package that would take a go code as a string and give me some information about the struct defined there

Or maybe there is a way to import the file that call go:generate to get directly access to the types

Upvotes: 4

Views: 2244

Answers (2)

Munaja
Munaja

Reputation: 149

Maybe you can utilize go/parser and go/ast in your generator.

Upvotes: 1

Mostafa Solati
Mostafa Solati

Reputation: 1235

There is no need to specify file name , this code does the same :

//go:generate go run ../generator.go -file $GOFILE

With help of text/template package you are needless of parsing the file. A very simple example would be something like this. This will give you the clue :

package main

import (
    "flag"
    "os"
    "text/template"
)

//go:generate go run main.go -name=A
//go:generate go run main.go -name=B
//go:generate go run main.go -name=C

var name = flag.String("name", "test", "name of struct")

var code = `
package main

type Struct{{.}} struct {}

func (s *Struct{{.}} ) Vailadte() bool {
return true
}
`

func main() {
    flag.Parse()
    file, _ := os.Create(*name + ".go")
    defer file.Close()

    tmpl, _ := template.New("test").Parse(code)
    tmpl.Execute(file, *name)
}

Upvotes: 1

Related Questions