bugfix fields

This commit is contained in:
Robert Janetzko 2022-04-14 17:06:52 +00:00
parent a9f3597edf
commit 757b7c6b79
6 changed files with 414 additions and 404 deletions

View File

@ -12,7 +12,7 @@ func main() {
flag.Parse() flag.Parse()
if len(*a) > 0 { if len(*a) > 0 {
df.Analyze(*a) df.AnalyzeStructure(*a)
} }
if *g { if *g {

View File

@ -1,13 +1,6 @@
package df package df
import ( import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"sort" "sort"
"strings" "strings"
@ -15,29 +8,18 @@ import (
"github.com/robertjanetzko/LegendsBrowser2/backend/util" "github.com/robertjanetzko/LegendsBrowser2/backend/util"
) )
func Analyze(filex string) error {
fmt.Println("Search...", filex)
files, err := filepath.Glob(filex + "/*.xml")
if err != nil {
return err
}
fmt.Println(files)
a := NewAnalyzeData()
for _, file := range files {
analyze(file, a)
}
return a.Save()
}
func Generate() error { func Generate() error {
a, err := LoadAnalyzeData() a, err := LoadAnalyzeData()
if err != nil { if err != nil {
return err return err
} }
return createMetadata(a)
m, err := createMetadata(a)
if err != nil {
return err
}
return generateCode(m)
} }
var allowedTyped = map[string]bool{ var allowedTyped = map[string]bool{
@ -45,58 +27,9 @@ var allowedTyped = map[string]bool{
"df_world|historical_event_collections|historical_event_collection": true, "df_world|historical_event_collections|historical_event_collection": true,
} }
func filterSubtypes(data *map[string]*FieldData) []string { type Metadata map[string]Object
filtered := make(map[string]*FieldData)
for k, v := range *data {
path := strings.Split(k, PATH_SEPARATOR)
for index, seg := range path {
if strings.Contains(seg, "+") {
base := seg[:strings.Index(seg, "+")]
basePath := strings.Join(append(path[:index], base), PATH_SEPARATOR)
if allowedTyped[basePath] {
path[index] = seg
}
}
}
filtered[strings.Join(path, PATH_SEPARATOR)] = v
}
*data = filtered
list := util.Keys(filtered)
sort.Strings(list)
return list
}
func getSubtypes(objectTypes []string, k string) *[]string { func createMetadata(a *AnalyzeData) (*Metadata, error) {
subtypes := make(map[string]bool)
for _, t := range objectTypes {
if strings.HasPrefix(t, k+"+") && !strings.Contains(t[len(k):], PATH_SEPARATOR) {
subtypes[t[strings.LastIndex(t, "+")+1:]] = true
}
}
keys := util.Keys(subtypes)
sort.Strings(keys)
if len(keys) > 0 {
return &keys
}
return nil
}
func getSubtypeOf(k string) *string {
if strings.Contains(k, PATH_SEPARATOR) {
last := k[strings.LastIndex(k, PATH_SEPARATOR)+1:]
if strings.Contains(last, "+") {
base := strcase.ToCamel(last[:strings.Index(last, "+")])
return &base
}
}
return nil
}
func createMetadata(a *AnalyzeData) error {
fs := filterSubtypes(&a.Fields) fs := filterSubtypes(&a.Fields)
var objectTypes []string var objectTypes []string
@ -107,7 +40,7 @@ func createMetadata(a *AnalyzeData) error {
} }
} }
objects := make(map[string]Object, 0) objects := make(Metadata, 0)
for _, k := range objectTypes { for _, k := range objectTypes {
if ok, _ := isArray(k, fs); !ok { if ok, _ := isArray(k, fs); !ok {
@ -171,34 +104,58 @@ func createMetadata(a *AnalyzeData) error {
} }
} }
return generateCode(&objects) return &objects, nil
} }
func generateCode(objects *map[string]Object) error { func filterSubtypes(data *map[string]*FieldData) []string {
file, _ := json.MarshalIndent(objects, "", " ") filtered := make(map[string]*FieldData)
_ = ioutil.WriteFile("model.json", file, 0644) for k, v := range *data {
path := strings.Split(k, PATH_SEPARATOR)
for index, seg := range path {
if strings.Contains(seg, "+") {
base := seg[:strings.Index(seg, "+")]
basePath := strings.Join(append(path[:index], base), PATH_SEPARATOR)
if allowedTyped[basePath] {
path[index] = seg
}
}
}
filtered[strings.Join(path, PATH_SEPARATOR)] = v
}
*data = filtered
list := util.Keys(filtered)
sort.Strings(list)
return list
}
f, err := os.Create("../backend/model/model.go") func getSubtypes(objectTypes []string, k string) *[]string {
if err != nil { subtypes := make(map[string]bool)
return err
}
defer f.Close()
var buf bytes.Buffer for _, t := range objectTypes {
err = packageTemplate.Execute(&buf, struct { if strings.HasPrefix(t, k+"+") && !strings.Contains(t[len(k):], PATH_SEPARATOR) {
Objects *map[string]Object subtypes[t[strings.LastIndex(t, "+")+1:]] = true
}{ }
Objects: objects,
})
if err != nil {
return err
} }
p, err := format.Source(buf.Bytes())
if err != nil { keys := util.Keys(subtypes)
return err sort.Strings(keys)
if len(keys) > 0 {
return &keys
} }
_, err = f.Write(p)
return err return nil
}
func getSubtypeOf(k string) *string {
if strings.Contains(k, PATH_SEPARATOR) {
last := k[strings.LastIndex(k, PATH_SEPARATOR)+1:]
if strings.Contains(last, "+") {
base := strcase.ToCamel(last[:strings.Index(last, "+")])
return &base
}
}
return nil
} }
func isArray(typ string, types []string) (bool, string) { func isArray(typ string, types []string) (bool, string) {

View File

@ -1,7 +1,12 @@
package df package df
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"go/format"
"io/ioutil"
"os"
"text/template" "text/template"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
@ -206,3 +211,30 @@ func parse{{ $obj.Name }}(d *xml.Decoder, start *xml.StartElement) (*{{ $obj.Nam
} }
{{- end }} {{- end }}
`)) `))
func generateCode(objects *Metadata) error {
file, _ := json.MarshalIndent(objects, "", " ")
_ = ioutil.WriteFile("model.json", file, 0644)
f, err := os.Create("../backend/model/model.go")
if err != nil {
return err
}
defer f.Close()
var buf bytes.Buffer
err = packageTemplate.Execute(&buf, struct {
Objects *Metadata
}{
Objects: objects,
})
if err != nil {
return err
}
p, err := format.Source(buf.Bytes())
if err != nil {
return err
}
_, err = f.Write(p)
return err
}

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -14,6 +15,23 @@ import (
"github.com/robertjanetzko/LegendsBrowser2/backend/util" "github.com/robertjanetzko/LegendsBrowser2/backend/util"
) )
func AnalyzeStructure(filex string) error {
fmt.Println("Search...", filex)
files, err := filepath.Glob(filex + "/*.xml")
if err != nil {
return err
}
fmt.Println(files)
a := NewAnalyzeData()
for _, file := range files {
analyze(file, a)
}
return a.Save()
}
type FieldData struct { type FieldData struct {
IsString bool IsString bool
Multiple bool Multiple bool
@ -85,8 +103,7 @@ const PATH_SEPARATOR = "|"
func analyzeElement(d *xml.Decoder, a *AnalyzeData, path []string, plus bool) error { func analyzeElement(d *xml.Decoder, a *AnalyzeData, path []string, plus bool) error {
if len(path) > 1 { if len(path) > 1 {
s := strings.Join(path, PATH_SEPARATOR) s := strings.Join(path, PATH_SEPARATOR)
fd := NewFieldData() fd := a.GetField(s)
a.Fields[s] = fd
if plus { if plus {
fd.Plus = true fd.Plus = true
} else { } else {
@ -116,7 +133,7 @@ Loop:
newPath := append(path, t.Name.Local) newPath := append(path, t.Name.Local)
if _, ok := fields[t.Name.Local]; ok { if _, ok := fields[t.Name.Local]; ok {
a.Fields[strings.Join(newPath, PATH_SEPARATOR)].Multiple = true a.GetField(strings.Join(newPath, PATH_SEPARATOR)).Multiple = true
} }
fields[t.Name.Local] = true fields[t.Name.Local] = true
@ -128,7 +145,7 @@ Loop:
case xml.EndElement: case xml.EndElement:
if value { if value {
if _, err := strconv.Atoi(string(data)); err != nil { if _, err := strconv.Atoi(string(data)); err != nil {
a.Fields[strings.Join(path, PATH_SEPARATOR)].IsString = true a.GetField(strings.Join(path, PATH_SEPARATOR)).IsString = true
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
{
"folders": [
{
"path": "."
},
{
"path": "backend"
},
{
"path": "analyze"
}
],
"settings": {}
}