2022-04-14 18:39:18 +03:00
|
|
|
package df
|
|
|
|
|
|
|
|
import (
|
2022-04-15 11:03:11 +03:00
|
|
|
"fmt"
|
2022-04-14 18:39:18 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/iancoleman/strcase"
|
|
|
|
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
|
|
|
|
)
|
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
func Generate() error {
|
|
|
|
a, err := LoadAnalyzeData()
|
2022-04-14 18:39:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
m, err := createMetadata(a)
|
2022-04-14 18:39:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-14 20:06:52 +03:00
|
|
|
|
2022-04-16 21:34:19 +03:00
|
|
|
if err := generateBackendCode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := generateFrontendCode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var allowedTyped = map[string]bool{
|
|
|
|
"df_world|historical_events|historical_event": true,
|
|
|
|
"df_world|historical_event_collections|historical_event_collection": true,
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:03:11 +03:00
|
|
|
func typeName(k string) string {
|
|
|
|
if strings.Contains(k, PATH_SEPARATOR) {
|
|
|
|
return k[strings.LastIndex(k, PATH_SEPARATOR)+1:]
|
|
|
|
}
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
type Metadata map[string]Object
|
2022-04-14 18:39:18 +03:00
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
func createMetadata(a *AnalyzeData) (*Metadata, error) {
|
2022-04-14 18:39:18 +03:00
|
|
|
fs := filterSubtypes(&a.Fields)
|
|
|
|
|
2022-04-15 11:03:11 +03:00
|
|
|
// unique type names
|
|
|
|
names := make(map[string]bool)
|
2022-04-14 18:39:18 +03:00
|
|
|
for k := range a.Fields {
|
|
|
|
path := strings.Split(k, PATH_SEPARATOR)
|
|
|
|
if len(path) >= 2 {
|
2022-04-15 11:03:11 +03:00
|
|
|
names[strings.Join(path[:len(path)-1], PATH_SEPARATOR)] = true
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 11:03:11 +03:00
|
|
|
objectTypes := util.Keys(names)
|
2022-04-14 18:39:18 +03:00
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
objects := make(Metadata, 0)
|
2022-04-15 11:03:11 +03:00
|
|
|
names = make(map[string]bool)
|
|
|
|
double := make(map[string]bool)
|
|
|
|
typeNames := make(map[string]string)
|
2022-04-14 18:39:18 +03:00
|
|
|
|
2022-04-15 11:03:11 +03:00
|
|
|
// check object type names
|
2022-04-14 18:39:18 +03:00
|
|
|
for _, k := range objectTypes {
|
2022-04-15 11:03:11 +03:00
|
|
|
n := typeName(k)
|
|
|
|
if _, ok := names[n]; ok {
|
|
|
|
double[n] = true
|
|
|
|
}
|
|
|
|
names[n] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range objectTypes {
|
|
|
|
typeNames[k] = strcase.ToCamel(typeName(k))
|
|
|
|
}
|
|
|
|
for _, n := range util.Keys(double) {
|
|
|
|
fmt.Println(n)
|
|
|
|
for _, k := range objectTypes {
|
|
|
|
if strings.HasSuffix(k, PATH_SEPARATOR+n) {
|
|
|
|
fmt.Println(" ", k)
|
|
|
|
path := strings.Split(k, PATH_SEPARATOR)
|
|
|
|
for i := len(path) - 1; i > 0; i-- {
|
|
|
|
sub := strings.Join(path[:i], PATH_SEPARATOR)
|
|
|
|
if ok, _ := isArray(sub, fs); !ok {
|
|
|
|
typeNames[k] = strcase.ToCamel(typeName(sub) + "_" + typeName(k))
|
|
|
|
fmt.Println(" ", typeNames[k])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
2022-04-15 11:03:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build metadata
|
|
|
|
for _, k := range objectTypes {
|
|
|
|
if ok, _ := isArray(k, fs); !ok {
|
|
|
|
n := typeName(k)
|
2022-04-14 18:39:18 +03:00
|
|
|
|
|
|
|
if n == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
objFields := make(map[string]Field, 0)
|
|
|
|
|
|
|
|
for _, f := range fs {
|
|
|
|
if strings.HasPrefix(f, k+PATH_SEPARATOR) {
|
|
|
|
fn := f[len(k)+1:]
|
|
|
|
if !strings.Contains(fn, PATH_SEPARATOR) {
|
|
|
|
legend := ""
|
|
|
|
if a.Fields[f].Base && a.Fields[f].Plus {
|
|
|
|
legend = "both"
|
|
|
|
} else if a.Fields[f].Base {
|
|
|
|
legend = "base"
|
|
|
|
} else if a.Fields[f].Plus {
|
|
|
|
legend = "plus"
|
|
|
|
}
|
|
|
|
|
|
|
|
field := Field{
|
|
|
|
Name: strcase.ToCamel(fn),
|
|
|
|
Type: "int",
|
|
|
|
Multiple: a.Fields[f].Multiple,
|
|
|
|
Legend: legend,
|
2022-04-15 16:27:51 +03:00
|
|
|
Base: a.Fields[f].Base,
|
|
|
|
Plus: a.Fields[f].Plus,
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
|
|
|
if ok, elements := isArray(f, fs); ok {
|
2022-04-15 11:03:11 +03:00
|
|
|
el := typeNames[elements]
|
2022-04-14 18:39:18 +03:00
|
|
|
if _, ok := a.Fields[elements+PATH_SEPARATOR+"id"]; ok {
|
|
|
|
field.Type = "map"
|
|
|
|
} else {
|
|
|
|
field.Type = "array"
|
|
|
|
}
|
|
|
|
field.ElementType = &(el)
|
|
|
|
} else if ok, _ := isObject(f, fs); ok {
|
|
|
|
field.Type = "object"
|
2022-04-15 11:03:11 +03:00
|
|
|
el := typeNames[f]
|
|
|
|
field.ElementType = &el
|
2022-04-16 21:34:19 +03:00
|
|
|
} else if !a.Fields[f].NoBool {
|
|
|
|
field.Type = "bool"
|
2022-04-14 18:39:18 +03:00
|
|
|
} else if a.Fields[f].IsString {
|
|
|
|
field.Type = "string"
|
|
|
|
}
|
|
|
|
objFields[fn] = field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:03:11 +03:00
|
|
|
objects[typeNames[k]] = Object{
|
|
|
|
Name: typeNames[k],
|
2022-04-14 18:39:18 +03:00
|
|
|
Id: a.Fields[k+PATH_SEPARATOR+"id"] != nil,
|
|
|
|
Named: a.Fields[k+PATH_SEPARATOR+"name"] != nil,
|
|
|
|
Typed: a.Fields[k+PATH_SEPARATOR+"type"] != nil,
|
2022-04-15 16:27:51 +03:00
|
|
|
SubTypes: getSubtypes(a, k),
|
2022-04-14 18:39:18 +03:00
|
|
|
SubTypeOf: getSubtypeOf(k),
|
|
|
|
Fields: objFields,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
return &objects, nil
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:06:52 +03:00
|
|
|
func filterSubtypes(data *map[string]*FieldData) []string {
|
|
|
|
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
|
|
|
|
}
|
2022-04-14 18:39:18 +03:00
|
|
|
|
2022-04-15 16:27:51 +03:00
|
|
|
func getSubtypes(a *AnalyzeData, k string) *[]Subtype {
|
|
|
|
if allowedTyped[k] {
|
|
|
|
if st, ok := a.SubTypes[k]; ok {
|
|
|
|
var list []Subtype
|
|
|
|
for _, v := range *st {
|
|
|
|
list = append(list, *v)
|
|
|
|
}
|
|
|
|
return &list
|
2022-04-14 20:06:52 +03:00
|
|
|
}
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
2022-04-14 20:06:52 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
2022-04-14 20:06:52 +03:00
|
|
|
return nil
|
2022-04-14 18:39:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func isArray(typ string, types []string) (bool, string) {
|
|
|
|
fc := 0
|
|
|
|
elements := ""
|
|
|
|
|
|
|
|
if !strings.Contains(typ, PATH_SEPARATOR) || strings.Contains(typ[strings.LastIndex(typ, PATH_SEPARATOR):], "+") {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range types {
|
|
|
|
if !strings.HasPrefix(t, typ+PATH_SEPARATOR) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(t[len(typ)+1:], PATH_SEPARATOR) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fc++
|
|
|
|
elements = t
|
|
|
|
}
|
|
|
|
return fc == 1, elements
|
|
|
|
}
|
|
|
|
|
|
|
|
func isObject(typ string, types []string) (bool, string) {
|
|
|
|
fc := 0
|
|
|
|
|
|
|
|
for _, t := range types {
|
|
|
|
if !strings.HasPrefix(t, typ+PATH_SEPARATOR) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fc++
|
|
|
|
}
|
|
|
|
return fc > 0, typ
|
|
|
|
}
|