vendredi 14 décembre 2018

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





Aucun commentaire:

Enregistrer un commentaire