Skip to content
metago is a meta programming library for go
Go Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd/metago
internal/testbed
.gitignore
LICENSE
README.md
apis.go
errors.go
go.mod
go.sum
main_test.go
processor.go
setup.sh
test.sh
tools.go
walker.go

README.md

metago

🚧 this project is under construction 🚧

metago is a meta programming library for go.

This idea is based on wire and V.

What is metago?

metago is work with markers.

Below code is a example about print field name & field value.

//+build metago

package main

import (
	"fmt"

	"github.com/vvakame/til/go/metago"
)

type Foo struct {
	ID   int64
	Name string
}

func main() {
	obj := &Foo{1, "vvakame"}
	mv := metago.ValueOf(obj)
	for _, mf := range mv.Fields() {
		fmt.Println(mf.Name(), mf.Value())
	}
}

This template code will be processed and generate below code. This code work actually.

// Code generated by metago. DO NOT EDIT.

//+build !metago

package main

import (
	"fmt"
)

type Foo struct {
	ID   int64
	Name string
}

func main() {
	obj := &Foo{1, "vvakame"}

	{
		fmt.Println("ID", obj.ID)
	}
	{
		fmt.Println("Name", obj.Name)
	}
}

metago package looks like reflect package. If you want to check example. see testbed directory.

Main function is ...

  • get metago.Value typed value by mv := metago.ValueOf(obj)
  • expand processes to each fields by for _, mf := range mv.Fields()
  • get field name by mf.Name()
  • get field value by mf.Value()
  • get field struct tag by something likes mf.StructTagGet("json")
  • eliminate statement by type assertion ( mf.Value().(time.Time) ) and condition statement
    • type switch support
  • define inline template. it has 1st argument is metago.Value types

How to install metago

$ go get -u github.com/vvakame/metago/cmd/metago
$ metago -v .

Motivation

Go is not much flexible for type and code. We must write a lot of boiler plate code. so, We want to generate code automatically.

Ideas

  1. construct AST by code and convert it to source code.

It is terrible ideas. AST is a data, not code. We want to write a code. not AST. It is very painful and not easy & convenience.

  1. construct go code by text.

It is common way to generate code. for example, jwg uses own Printf function to construct code. gqlgen uses text/template package.

It is easy way, but We can't get support from IDE. IDE can't understand some text is go code or not. We must required that run & compile to find some errors.

  1. translate go code to other go code.

metago uses some "meta" go code. It is a valid go code, but it is template. parse template code to AST. and AST will be convert to another go code.

TODO

  • improve test cases
  • generate new struct types
  • generate method definitions
You can’t perform that action at this time.