unique type names

This commit is contained in:
Robert Janetzko 2022-04-15 08:03:11 +00:00
parent 757b7c6b79
commit a40e947e60
4 changed files with 1737 additions and 1407 deletions

View File

@ -1,6 +1,7 @@
package df package df
import ( import (
"fmt"
"sort" "sort"
"strings" "strings"
@ -27,27 +28,68 @@ var allowedTyped = map[string]bool{
"df_world|historical_event_collections|historical_event_collection": true, "df_world|historical_event_collections|historical_event_collection": true,
} }
func typeName(k string) string {
if strings.Contains(k, PATH_SEPARATOR) {
return k[strings.LastIndex(k, PATH_SEPARATOR)+1:]
}
return k
}
type Metadata map[string]Object type Metadata map[string]Object
func createMetadata(a *AnalyzeData) (*Metadata, error) { func createMetadata(a *AnalyzeData) (*Metadata, error) {
fs := filterSubtypes(&a.Fields) fs := filterSubtypes(&a.Fields)
var objectTypes []string // unique type names
names := make(map[string]bool)
for k := range a.Fields { for k := range a.Fields {
path := strings.Split(k, PATH_SEPARATOR) path := strings.Split(k, PATH_SEPARATOR)
if len(path) >= 2 { if len(path) >= 2 {
objectTypes = append(objectTypes, strings.Join(path[:len(path)-1], PATH_SEPARATOR)) names[strings.Join(path[:len(path)-1], PATH_SEPARATOR)] = true
} }
} }
objectTypes := util.Keys(names)
objects := make(Metadata, 0) objects := make(Metadata, 0)
names = make(map[string]bool)
double := make(map[string]bool)
typeNames := make(map[string]string)
// check object type names
for _, k := range objectTypes {
n := typeName(k)
if _, ok := names[n]; ok {
double[n] = true
}
names[n] = true
}
for _, k := range objectTypes { for _, k := range objectTypes {
if ok, _ := isArray(k, fs); !ok { typeNames[k] = strcase.ToCamel(typeName(k))
n := k
if strings.Contains(k, PATH_SEPARATOR) {
n = k[strings.LastIndex(k, PATH_SEPARATOR)+1:]
} }
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
}
}
}
}
}
// build metadata
for _, k := range objectTypes {
if ok, _ := isArray(k, fs); !ok {
n := typeName(k)
if n == "" { if n == "" {
continue continue
@ -75,7 +117,7 @@ func createMetadata(a *AnalyzeData) (*Metadata, error) {
Legend: legend, Legend: legend,
} }
if ok, elements := isArray(f, fs); ok { if ok, elements := isArray(f, fs); ok {
el := elements[strings.LastIndex(elements, PATH_SEPARATOR)+1:] el := typeNames[elements]
if _, ok := a.Fields[elements+PATH_SEPARATOR+"id"]; ok { if _, ok := a.Fields[elements+PATH_SEPARATOR+"id"]; ok {
field.Type = "map" field.Type = "map"
} else { } else {
@ -84,6 +126,8 @@ func createMetadata(a *AnalyzeData) (*Metadata, error) {
field.ElementType = &(el) field.ElementType = &(el)
} else if ok, _ := isObject(f, fs); ok { } else if ok, _ := isObject(f, fs); ok {
field.Type = "object" field.Type = "object"
el := typeNames[f]
field.ElementType = &el
} else if a.Fields[f].IsString { } else if a.Fields[f].IsString {
field.Type = "string" field.Type = "string"
} }
@ -92,8 +136,8 @@ func createMetadata(a *AnalyzeData) (*Metadata, error) {
} }
} }
objects[n] = Object{ objects[typeNames[k]] = Object{
Name: strcase.ToCamel(n), Name: typeNames[k],
Id: a.Fields[k+PATH_SEPARATOR+"id"] != nil, Id: a.Fields[k+PATH_SEPARATOR+"id"] != nil,
Named: a.Fields[k+PATH_SEPARATOR+"name"] != nil, Named: a.Fields[k+PATH_SEPARATOR+"name"] != nil,
Typed: a.Fields[k+PATH_SEPARATOR+"type"] != nil, Typed: a.Fields[k+PATH_SEPARATOR+"type"] != nil,

View File

@ -30,7 +30,7 @@ type Field struct {
Legend string `json:"legend"` Legend string `json:"legend"`
} }
func (f Field) TypeLine(objects map[string]Object) string { func (f Field) TypeLine() string {
n := f.Name n := f.Name
if n == "Id" || n == "Name" { if n == "Id" || n == "Name" {
@ -43,13 +43,13 @@ func (f Field) TypeLine(objects map[string]Object) string {
} }
t := f.Type t := f.Type
if f.Type == "array" { if f.Type == "array" {
t = "[]*" + objects[*f.ElementType].Name t = "[]*" + *f.ElementType
} }
if f.Type == "map" { if f.Type == "map" {
t = "map[int]*" + objects[*f.ElementType].Name t = "map[int]*" + *f.ElementType
} }
if f.Type == "object" { if f.Type == "object" {
t = "*" + f.Name t = "*" + *f.ElementType
} }
j := fmt.Sprintf("`json:\"%s\" legend:\"%s\"`", strcase.ToLowerCamel(f.Name), f.Legend) j := fmt.Sprintf("`json:\"%s\" legend:\"%s\"`", strcase.ToLowerCamel(f.Name), f.Legend)
return fmt.Sprintf("%s %s%s %s", n, m, t, j) return fmt.Sprintf("%s %s%s %s", n, m, t, j)
@ -72,15 +72,14 @@ func (f Field) StartAction() string {
} }
if f.Type == "array" || f.Type == "map" { if f.Type == "array" || f.Type == "map" {
el := strcase.ToCamel(*f.ElementType) gen := fmt.Sprintf("parse%s", *f.ElementType)
gen := fmt.Sprintf("parse%s", el)
if f.Type == "array" { if f.Type == "array" {
return fmt.Sprintf("parseArray(d, &obj.%s, %s)", f.Name, gen) return fmt.Sprintf("parseArray(d, &obj.%s, %s)", f.Name, gen)
} }
if f.Type == "map" { if f.Type == "map" {
return fmt.Sprintf("obj.%s = make(map[int]*%s)\nparseMap(d, &obj.%s, %s)", f.Name, el, f.Name, gen) return fmt.Sprintf("obj.%s = make(map[int]*%s)\nparseMap(d, &obj.%s, %s)", f.Name, *f.ElementType, f.Name, gen)
} }
} }
@ -128,7 +127,7 @@ import (
type {{ $obj.Name }} struct { type {{ $obj.Name }} struct {
{{- range $fname, $field := $obj.Fields }} {{- range $fname, $field := $obj.Fields }}
{{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }} {{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }}
{{ $field.TypeLine $.Objects }} {{ $field.TypeLine }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- if not (not $obj.SubTypes) }} {{- if not (not $obj.SubTypes) }}

File diff suppressed because it is too large Load Diff

View File

@ -3,12 +3,15 @@
{ {
"path": "." "path": "."
}, },
{
"path": "analyze"
},
{ {
"path": "backend" "path": "backend"
}, },
{ {
"path": "analyze" "path": "frontend"
} },
], ],
"settings": {} "settings": {}
} }