analyze cleanup

This commit is contained in:
Robert Janetzko 2022-04-18 15:26:14 +00:00
parent 341cf8142f
commit 821a1439a7
5 changed files with 23 additions and 110 deletions

View File

@ -10,6 +10,7 @@ import (
func main() {
a := flag.String("a", "", "analyze a file")
g := flag.Bool("g", false, "generate model")
e := flag.Bool("e", false, "regenerate events")
flag.Parse()
if len(*a) > 0 {
@ -17,9 +18,26 @@ func main() {
}
if *g {
err := df.Generate()
a, err := df.LoadAnalyzeData()
if err != nil {
log.Fatal(err)
}
df.ListEnumCandidates(a)
m, err := df.CreateMetadata(a)
if err != nil {
log.Fatal(err)
}
if err := df.GenerateBackendCode(m); err != nil {
log.Fatal(err)
}
if *e {
if err := df.GenerateEventsCode(m); err != nil {
log.Fatal(err)
}
}
}
}

View File

@ -9,34 +9,6 @@ import (
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
)
func Generate() error {
a, err := LoadAnalyzeData()
if err != nil {
return err
}
listEnumCandidates(a)
m, err := createMetadata(a)
if err != nil {
return err
}
if err := generateBackendCode(m); err != nil {
return err
}
if err := generateEventsCode(m); err != nil {
return err
}
if err := generateFrontendCode(m); err != nil {
return err
}
return nil
}
var allowedTyped = map[string]bool{
"df_world|historical_events|historical_event": true,
"df_world|historical_event_collections|historical_event_collection": true,
@ -51,7 +23,7 @@ func typeName(k string) string {
type Metadata map[string]Object
func createMetadata(a *AnalyzeData) (*Metadata, error) {
func CreateMetadata(a *AnalyzeData) (*Metadata, error) {
fs := filterSubtypes(&a.Fields)
// unique type names
@ -257,7 +229,7 @@ func isObject(typ string, types []string) (bool, string) {
return fc > 0, typ
}
func listEnumCandidates(a *AnalyzeData) {
func ListEnumCandidates(a *AnalyzeData) {
keys := util.Keys(a.Fields)
sort.Strings(keys)

View File

@ -251,7 +251,7 @@ func LoadSameFields() error {
return nil
}
func generateBackendCode(objects *Metadata) error {
func GenerateBackendCode(objects *Metadata) error {
LoadSameFields()
file, _ := json.MarshalIndent(objects, "", " ")

View File

@ -20,7 +20,7 @@ func (x *{{ $obj.Name }}) Html() string { return "UNKNWON {{ $obj.Name }}" }
{{- end }}
`))
func generateEventsCode(objects *Metadata) error {
func GenerateEventsCode(objects *Metadata) error {
file, _ := json.MarshalIndent(objects, "", " ")
_ = ioutil.WriteFile("model.json", file, 0644)

View File

@ -1,77 +0,0 @@
package df
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"text/template"
"github.com/iancoleman/strcase"
)
var frontendTemplate = template.Must(template.New("").Parse(`// Code generated by legendsbrowser; DO NOT EDIT.
{{- range $name, $obj := $.Objects }}
export interface {{ $obj.Name }} {
{{- range $fname, $field := $obj.Fields }}
{{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }}
{{ $field.JsonTypeLine }};
{{- end }}
{{- end }}
{{- if not (not $obj.SubTypes) }}
Details: any;
{{- end }}
}
{{- end }}
`))
func generateFrontendCode(objects *Metadata) error {
file, _ := json.MarshalIndent(objects, "", " ")
_ = ioutil.WriteFile("model.json", file, 0644)
f, err := os.Create("../frontend/src/app/types.ts")
if err != nil {
return err
}
defer f.Close()
var buf bytes.Buffer
err = frontendTemplate.Execute(&buf, struct {
Objects *Metadata
Modes []bool
}{
Objects: objects,
Modes: []bool{false, true},
})
if err != nil {
return err
}
_, err = f.Write(buf.Bytes())
return err
}
func (f Field) JsonTypeLine() string {
m := ""
if f.Multiple {
m = "[]"
}
t := f.Type
if f.Type == "int" {
t = "number"
}
if f.Type == "bool" {
t = "boolean"
}
if f.Type == "array" {
t = *f.ElementType + "[]"
}
if f.Type == "map" {
t = "{ [key:number]:" + *f.ElementType + "; }"
}
if f.Type == "object" {
t = *f.ElementType
}
return fmt.Sprintf("%s: %s%s", strcase.ToLowerCamel(f.Name), t, m)
}