Convert to templates
Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
@ -6,136 +6,18 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/token"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
const (
|
||||
infoURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.17/items.json"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
ID uint32 `json:"id"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Name string `json:"name"`
|
||||
StackSize uint `json:"stackSize"`
|
||||
}
|
||||
|
||||
func downloadInfo() ([]Item, error) {
|
||||
resp, err := http.Get(infoURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var data []Item
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func makeItemDeclaration(blocks []Item) *ast.DeclStmt {
|
||||
out := &ast.DeclStmt{Decl: &ast.GenDecl{Tok: token.VAR}}
|
||||
|
||||
for _, b := range blocks {
|
||||
t := reflect.TypeOf(b)
|
||||
fields := make([]ast.Expr, t.NumField())
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
ft := t.Field(i)
|
||||
|
||||
var val ast.Expr
|
||||
switch ft.Type.Kind() {
|
||||
case reflect.Uint32, reflect.Int, reflect.Uint:
|
||||
val = &ast.BasicLit{Kind: token.INT, Value: fmt.Sprint(reflect.ValueOf(b).Field(i))}
|
||||
case reflect.Float64:
|
||||
val = &ast.BasicLit{Kind: token.FLOAT, Value: fmt.Sprint(reflect.ValueOf(b).Field(i))}
|
||||
case reflect.String:
|
||||
val = &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(reflect.ValueOf(b).Field(i).String())}
|
||||
case reflect.Bool:
|
||||
val = &ast.BasicLit{Kind: token.IDENT, Value: fmt.Sprint(reflect.ValueOf(b).Field(i).Bool())}
|
||||
|
||||
case reflect.Slice:
|
||||
val = &ast.CompositeLit{
|
||||
Type: &ast.ArrayType{
|
||||
Elt: &ast.BasicLit{Kind: token.IDENT, Value: ft.Type.Elem().Name()},
|
||||
},
|
||||
}
|
||||
v := reflect.ValueOf(b).Field(i)
|
||||
switch ft.Type.Elem().Kind() {
|
||||
case reflect.Uint32, reflect.Int:
|
||||
for x := 0; x < v.Len(); x++ {
|
||||
val.(*ast.CompositeLit).Elts = append(val.(*ast.CompositeLit).Elts, &ast.BasicLit{
|
||||
Kind: token.INT,
|
||||
Value: fmt.Sprint(v.Index(x)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Map:
|
||||
// Must be the NeedsTools map of type map[uint32]bool.
|
||||
m := &ast.CompositeLit{
|
||||
Type: &ast.MapType{
|
||||
Key: &ast.BasicLit{Kind: token.IDENT, Value: ft.Type.Key().Name()},
|
||||
Value: &ast.BasicLit{Kind: token.IDENT, Value: ft.Type.Elem().Name()},
|
||||
},
|
||||
}
|
||||
iter := reflect.ValueOf(b).Field(i).MapRange()
|
||||
for iter.Next() {
|
||||
m.Elts = append(m.Elts, &ast.KeyValueExpr{
|
||||
Key: &ast.BasicLit{Kind: token.INT, Value: fmt.Sprint(iter.Key().Uint())},
|
||||
Value: &ast.BasicLit{Kind: token.IDENT, Value: fmt.Sprint(iter.Value().Bool())},
|
||||
})
|
||||
}
|
||||
|
||||
val = m
|
||||
}
|
||||
|
||||
fields[i] = &ast.KeyValueExpr{
|
||||
Key: &ast.Ident{Name: ft.Name},
|
||||
Value: val,
|
||||
}
|
||||
}
|
||||
|
||||
out.Decl.(*ast.GenDecl).Specs = append(out.Decl.(*ast.GenDecl).Specs, &ast.ValueSpec{
|
||||
Names: []*ast.Ident{{Name: strcase.ToCamel(b.Name)}},
|
||||
Values: []ast.Expr{
|
||||
&ast.CompositeLit{
|
||||
Type: &ast.Ident{Name: reflect.TypeOf(b).Name()},
|
||||
Elts: fields,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
//go:generate go run $GOFILE
|
||||
//go:generate go fmt item.go
|
||||
func main() {
|
||||
items, err := downloadInfo()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
f, err := os.Create("item.go")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fmt.Fprintln(f, `// Package item stores information about items in Minecraft.
|
||||
//language=gohtml
|
||||
itemTmpl = `// Code generated by gen_item.go DO NOT EDIT.
|
||||
// Package item stores information about items in Minecraft.
|
||||
package item
|
||||
|
||||
// ID describes the numeric ID of an item.
|
||||
@ -149,24 +31,65 @@ type Item struct {
|
||||
StackSize uint
|
||||
}
|
||||
|
||||
`)
|
||||
format.Node(f, token.NewFileSet(), makeItemDeclaration(items))
|
||||
var (
|
||||
{{- range .}}
|
||||
{{.CamelName}} = Item{
|
||||
ID: {{.ID}},
|
||||
DisplayName: "{{.DisplayName}}",
|
||||
Name: "{{.Name}}",
|
||||
StackSize: {{.StackSize}},
|
||||
}{{end}}
|
||||
)
|
||||
|
||||
fmt.Fprintln(f)
|
||||
fmt.Fprintln(f)
|
||||
fmt.Fprintln(f, "// ByID is an index of minecraft items by their ID.")
|
||||
fmt.Fprintln(f, "var ByID = map[ID]*Item{")
|
||||
for _, i := range items {
|
||||
fmt.Fprintf(f, " %d: &%s,\n", i.ID, strcase.ToCamel(i.Name))
|
||||
}
|
||||
fmt.Fprintln(f, "}")
|
||||
fmt.Fprintln(f)
|
||||
// ByID is an index of minecraft items by their ID.
|
||||
var ByID = map[ID]*Item{ {{range .}}
|
||||
{{.ID}}: &{{.CamelName}},{{end}}
|
||||
}`
|
||||
)
|
||||
|
||||
fmt.Fprintln(f, "// ByName is an index of minecraft items by their name.")
|
||||
fmt.Fprintln(f, "var ByName = map[string]*Item{")
|
||||
for _, i := range items {
|
||||
fmt.Fprintf(f, " %q: &%s,\n", i.Name, strcase.ToCamel(i.Name))
|
||||
}
|
||||
fmt.Fprintln(f, "}")
|
||||
fmt.Fprintln(f)
|
||||
type Item struct {
|
||||
ID uint32 `json:"id"`
|
||||
CamelName string `json:"-"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Name string `json:"name"`
|
||||
StackSize uint `json:"stackSize"`
|
||||
}
|
||||
|
||||
func downloadInfo() ([]*Item, error) {
|
||||
resp, err := http.Get(infoURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var data []*Item
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, d := range data {
|
||||
d.CamelName = strcase.ToCamel(d.Name)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
//go:generate go run $GOFILE
|
||||
//go:generate go fmt item.go
|
||||
func main() {
|
||||
fmt.Println("generating item.go")
|
||||
items, err := downloadInfo()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
f, err := os.Create("item.go")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err := template.Must(template.New("").Parse(itemTmpl)).Execute(f, items); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user