vvakame / metago

metago is a meta programming library for go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

metago is a meta programming library for go

License:MIT License


Languages

Language:Go 98.3%Language:Shell 1.7%