-
Notifications
You must be signed in to change notification settings - Fork 3
/
generators.go
85 lines (72 loc) · 2.05 KB
/
generators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mcdata
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ChimeraCoder/gojson"
"github.com/hashicorp/go-multierror"
)
var (
ErrVersionNotSupported = errors.New("version provided not supported")
)
func packageNameFromPath(p string) string {
ps := strings.Split(p, "/")
return ps[len(ps)-1]
}
func GenerateStructs(edition, version, dest string) error {
dataPaths, err := LoadDataPaths()
if err != nil {
return err
}
versionedPath, exist := dataPaths.getVersionedPaths(edition, version)
if !exist {
return ErrVersionNotSupported
}
curDir, _ := os.Getwd()
basepath := filepath.Join(curDir, SubmoduleDataPath) // Refactor to read from embeded
outpath := filepath.Join(curDir, dest)
if err := os.MkdirAll(outpath, 0777); err != nil {
if !os.IsExist(err) {
return err
}
}
fmt.Printf("mcdata (%d) entities detected\n", len(versionedPath))
var res error
var counter int
for datatype, datapath := range versionedPath {
counter++
datafilepath := filepath.Join(basepath, datapath, datatype+".json")
datafile, err := os.Open(datafilepath)
if err != nil {
// "Should" never happen, since dataPath definitions will always reflect a json file
// within its respective version. Else an error should be filed to Prismarine:
// https://github.com/PrismarineJS/minecraft-data/issues
multierror.Append(res, err)
continue
}
structName := strings.Title(datatype)
packageName := packageNameFromPath(outpath)
genfile, err := gojson.Generate(datafile, gojson.ParseJson, structName, packageName, []string{"json"}, false, true)
if err != nil {
multierror.Append(res, err)
continue
}
structFile := filepath.Join(outpath, datatype+".go")
f, err := os.Create(structFile)
if err != nil {
multierror.Append(res, err)
continue
}
defer f.Close()
_, err = f.Write(genfile)
if err != nil {
multierror.Append(res, err)
fmt.Printf("Failed to generate entity: (%d) %s\n", counter, datatype)
} else {
fmt.Printf("Generated go struct for entity: (%d) %s\n", counter, datatype)
}
}
return res
}