dorfylegends/analyze/df/generate_backend.go

614 lines
17 KiB
Go
Raw Permalink Normal View History

2022-04-16 21:34:19 +03:00
package df
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"os"
2022-04-24 11:58:31 +03:00
"regexp"
2022-04-18 14:39:28 +03:00
"sort"
2022-04-16 23:12:23 +03:00
"strings"
2022-04-16 21:34:19 +03:00
"text/template"
"github.com/iancoleman/strcase"
2022-04-16 23:12:23 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-04-16 21:34:19 +03:00
)
2022-04-30 10:04:33 +03:00
func enumValue(s string) string {
return strcase.ToCamel(strings.ReplaceAll(strings.ReplaceAll(s, "2", " two"), ":", "_"))
}
2022-04-18 14:39:28 +03:00
func enumString(s string) string { return strcase.ToDelimited(s, ' ') }
func startswith(str, prefix string) bool {
return strings.HasPrefix(str, prefix)
}
2022-04-18 14:39:28 +03:00
var backendTemplate = template.Must(template.New("").Funcs(template.FuncMap{
"enum": enumValue,
"enumString": enumString,
"startswith": startswith,
2022-04-18 14:39:28 +03:00
}).Parse(`// Code generated by legendsbrowser; DO NOT EDIT.
2022-04-16 21:34:19 +03:00
package model
import (
2022-04-19 21:32:20 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-04-16 21:34:19 +03:00
"fmt"
2022-04-18 14:39:28 +03:00
"encoding/json"
2022-04-16 21:34:19 +03:00
)
2022-04-18 11:36:29 +03:00
func InitSameFields() {
sameFields = map[string]map[string]map[string]bool{
{{- range $name, $obj := $.Objects }}
"{{$obj.Name}}": {
{{- range $field := ($obj.LegendFields "plus") }}
{{- if ne 0 (len ($obj.LegendFields "base")) }}
"{{$field.Name}}": {
{{- range $field2 := ($obj.LegendFields "base") }}
{{- if eq $field.Type $field2.Type }}
"{{ $field2.Name }}": true,
{{- end }}
{{- end }}
},
{{- end }}
{{- end }}
},
{{- end }}
}
}
2022-04-16 21:34:19 +03:00
{{- range $name, $obj := $.Objects }}
2022-04-18 14:39:28 +03:00
{{- range $fname, $field := $obj.Fields }}
{{- if eq $field.Type "enum" }}
type {{ $obj.Name }}{{ $field.Name }} int
const (
{{ $obj.Name }}{{ $field.Name }}_Unknown {{ $obj.Name }}{{ $field.Name }} = iota
{{- range $i, $v := $field.UniqueEnumValues }}
{{ $obj.Name }}{{ $field.Name }}_{{ enum $v }}
{{- end }}
)
func parse{{ $obj.Name }}{{ $field.Name }}(s string) {{ $obj.Name }}{{ $field.Name }} {
switch s {
{{- range $i, $v := $field.EnumValues }}
case "{{ $v }}":
return {{ $obj.Name }}{{ $field.Name }}_{{ enum $v }}
{{- end }}
}
return {{ $obj.Name }}{{ $field.Name }}_Unknown
}
func (s {{ $obj.Name }}{{ $field.Name }}) String() string {
switch s {
{{- range $i, $v := $field.UniqueEnumValues }}
case {{ $obj.Name }}{{ $field.Name }}_{{ enum $v }}:
return "{{ enumString $v }}"
{{- end }}
}
return "unknown"
}
func (s {{ $obj.Name }}{{ $field.Name }}) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
{{- end }}
{{- end }}
2022-04-16 21:34:19 +03:00
type {{ $obj.Name }} struct {
{{- range $fname, $field := $obj.Fields }}
{{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }}
2022-04-18 11:36:29 +03:00
{{- if not ($field.SameField $obj) }}
{{ $field.TypeLine }} // {{ $fname }}
{{- end }}
2022-04-16 21:34:19 +03:00
{{- end }}
{{- end }}
{{- if not (not $obj.SubTypes) }}
2022-04-16 23:12:23 +03:00
Details {{ $obj.Name }}Details
2022-04-16 21:34:19 +03:00
{{- end }}
2022-04-29 15:21:27 +03:00
{{- range $fname, $field := $obj.Additional }}
{{ $field.TypeLine }} // {{ $fname }}
{{- end }}
2022-04-16 21:34:19 +03:00
}
2022-04-21 11:58:28 +03:00
func New{{ $obj.Name }}() *{{ $obj.Name }} {
return &{{ $obj.Name }}{
{{- range $fname, $field := $obj.Fields }}{{- if $field.MustInit }}{{- if not ($field.SameField $obj) }}
{{ $field.Init }}
{{- end }}{{- end }}{{- end }}
2022-04-29 16:28:08 +03:00
{{- range $fname, $field := $obj.Additional }}{{- if $field.MustInit }}
{{ $field.Init }}
{{- end }}{{- end }}
2022-04-21 11:58:28 +03:00
}
}
2022-04-16 21:34:19 +03:00
{{- if $obj.Id }}
func (x *{{ $obj.Name }}) Id() int { return x.Id_ }
2022-04-28 22:24:55 +03:00
func (x *{{ $obj.Name }}) setId(id int) { x.Id_ = id }
2022-04-16 21:34:19 +03:00
{{- end }}
{{- if $obj.Named }}
func (x *{{ $obj.Name }}) Name() string { return x.Name_ }
{{- end }}
2022-04-19 14:25:04 +03:00
{{- if $obj.SubType }}
func (x *{{ $obj.Name }}) Type() string { return "{{ $obj.SubType }}" }
{{- end }}
2022-05-03 22:39:00 +03:00
{{- if $obj.SubType }}
2022-04-24 11:58:31 +03:00
func (x *{{ $obj.Name }}) RelatedToEntity(id int) bool { return {{ $obj.RelatedToEntity }} }
func (x *{{ $obj.Name }}) RelatedToHf(id int) bool { return {{ $obj.RelatedToHf }} }
func (x *{{ $obj.Name }}) RelatedToArtifact(id int) bool { return {{ $obj.RelatedToArtifact }} }
func (x *{{ $obj.Name }}) RelatedToSite(id int) bool { return {{ $obj.RelatedToSite }} }
2022-04-30 11:25:50 +03:00
func (x *{{ $obj.Name }}) RelatedToStructure(siteId, id int) bool { return {{ $obj.RelatedToStructure }} }
2022-04-24 11:58:31 +03:00
func (x *{{ $obj.Name }}) RelatedToRegion(id int) bool { return {{ $obj.RelatedToRegion }} }
2022-05-03 22:39:00 +03:00
func (x *{{ $obj.Name }}) RelatedToWorldConstruction(id int) bool { return {{ $obj.RelatedToWorldConstruction }} }
func (x *{{ $obj.Name }}) RelatedToWrittenContent(id int) bool { return {{ $obj.RelatedToWrittenContent }} }
func (x *{{ $obj.Name }}) RelatedToDanceForm(id int) bool { return {{ $obj.RelatedToDanceForm }} }
func (x *{{ $obj.Name }}) RelatedToMusicalForm(id int) bool { return {{ $obj.RelatedToMusicalForm }} }
func (x *{{ $obj.Name }}) RelatedToPoeticForm(id int) bool { return {{ $obj.RelatedToPoeticForm }} }
2022-05-04 15:07:35 +03:00
func (x *{{ $obj.Name }}) RelatedToMountain(id int) bool { return {{ $obj.RelatedToMountain }} }
2022-05-07 22:17:05 +03:00
func (x *{{ $obj.Name }}) RelatedToIdentity(id int) bool { return {{ $obj.RelatedToIdentity }} }
2022-05-03 22:39:00 +03:00
{{- end }}
2022-04-18 11:36:29 +03:00
func (x *{{ $obj.Name }}) CheckFields() {
{{- range $field := ($obj.LegendFields "plus") }}
{{- if not ($field.SameField $obj) }}
{{- range $field2 := ($obj.LegendFields "base") }}
{{- if eq $field.Type $field2.Type }}
{{- if eq $field.Type "int" }}
2022-04-22 18:45:10 +03:00
if x.{{ $field.Name}} != x.{{ $field2.Name}} {
2022-04-18 11:36:29 +03:00
sameFields["{{$obj.Name}}"]["{{ $field.Name}}"]["{{ $field2.Name}}"] = false
}
{{- end }}
{{- if eq $field.Type "string" }}
if x.{{ $field.Name}} != x.{{ $field2.Name}} && x.{{ $field.Name}} != "" && x.{{ $field2.Name}} != "" {
sameFields["{{$obj.Name}}"]["{{ $field.Name}}"]["{{ $field2.Name}}"] = false
}
{{- end }} {{- end }}
{{- end }}
{{- end }}
{{- end }}
}
2022-04-21 11:58:28 +03:00
func (x *{{ $obj.Name }}) MarshalJSON() ([]byte, error) {
d := make(map[string]any)
{{- range $fname, $field := $obj.Fields }}{{- if not ($field.SameField $obj) }}{{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }}
{{ $field.JsonMarshal }}
{{- end }}{{- end }}{{- end }}
2022-05-03 15:59:47 +03:00
{{- if not (not $obj.SubTypes) }}
d["details"] = x.Details
{{- end }}
{{- range $fname, $field := $obj.Additional }}
{{ $field.JsonMarshal }}
{{- end }}
2022-04-21 11:58:28 +03:00
return json.Marshal(d)
}
2022-04-16 21:34:19 +03:00
{{- end }}
// Parser
{{- range $name, $obj := $.Objects }}
{{- range $plus := $.Modes }}
2022-04-19 21:32:20 +03:00
func parse{{ $obj.Name }}{{ if $plus }}Plus{{ end }}(p *util.XMLParser{{ if $plus }}, obj *{{ $obj.Name }}{{ end }}) (*{{ $obj.Name }}, error) {
2022-04-21 11:58:28 +03:00
{{- if not $plus }}
var obj = New{{ $obj.Name }}()
{{- end }}
2022-04-16 21:34:19 +03:00
{{- if $plus }}
if obj == nil {
2022-04-21 11:58:28 +03:00
obj = New{{ $obj.Name }}()
2022-04-16 21:34:19 +03:00
}
{{- end }}
for {
2022-04-19 21:32:20 +03:00
t, n, err := p.Token()
2022-04-16 21:34:19 +03:00
if err != nil {
return nil, err
}
2022-04-19 21:32:20 +03:00
switch t {
case util.StartElement:
switch n {
2022-04-16 21:34:19 +03:00
{{- range $fname, $field := $obj.Fields }}
{{- if $field.Active $plus }}
case "{{ $fname }}":
2022-04-19 21:32:20 +03:00
{{- if and (eq $fname "type") (not (not $obj.SubTypes)) }}
data, err := p.Value()
if err != nil {
return nil, err
}
2022-04-20 13:46:42 +03:00
switch string(data) {
2022-04-19 21:32:20 +03:00
{{- range $sub := ($obj.ActiveSubTypes $plus) }}
case "{{ $sub.Case }}":
{{- if eq 1 (len $sub.Options) }}
{{- if not $plus }}
obj.Details, err = parse{{ $sub.Name }}(p)
{{- else }}
obj.Details, err = parse{{ $sub.Name }}Plus(p, obj.Details.(*{{ $sub.Name }}))
{{- end }}
{{- else }}
switch details := obj.Details.(type) {
{{- range $opt := $sub.Options }}
case *{{ $opt}}:
obj.Details, err = parse{{ $opt }}Plus(p, details)
{{- end }}
default:
fmt.Println("unknown subtype option", obj.Details)
p.Skip()
}
{{- end }}
{{- end }}
default:
{{- if not (startswith $obj.Name "HistoricalEventCollection") }}
2023-11-17 16:50:14 +02:00
obj.Details = NewHistoricalEventUnknown(string(data))
{{- end }}
2022-04-19 21:32:20 +03:00
p.Skip()
}
if err != nil {
return nil, err
}
return obj, nil
{{- else }}
{{ $field.StartAction $obj $plus }}
{{- end }}
{{- end }}
{{- end }}
2022-04-16 21:34:19 +03:00
default:
2022-04-19 21:32:20 +03:00
// fmt.Println("unknown field", n)
p.Skip()
2022-04-16 21:34:19 +03:00
}
2022-04-19 21:32:20 +03:00
case util.EndElement:
2022-04-18 11:36:29 +03:00
obj.CheckFields()
2022-04-16 21:34:19 +03:00
return obj, nil
}
}
}
{{- end }}
{{- end }}
`))
2022-04-18 11:36:29 +03:00
var sameFields map[string]map[string]string
func LoadSameFields() error {
data, err := ioutil.ReadFile("same.json")
if err != nil {
return err
}
sameFields = make(map[string]map[string]string)
json.Unmarshal(data, &sameFields)
return nil
}
2022-04-18 18:26:14 +03:00
func GenerateBackendCode(objects *Metadata) error {
2022-04-16 21:34:19 +03:00
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 = backendTemplate.Execute(&buf, struct {
Objects *Metadata
Modes []bool
}{
Objects: objects,
Modes: []bool{false, true},
})
if err != nil {
return err
}
p, err := format.Source(buf.Bytes())
if err != nil {
fmt.Println("WARN: could not format source", err)
p = buf.Bytes()
}
_, err = f.Write(p)
return err
}
2022-04-27 22:44:39 +03:00
func (f Field) FixedName() string {
2022-04-16 21:34:19 +03:00
n := f.Name
2022-04-27 22:44:39 +03:00
if n == "Id" || n == "Name" || n == "Type" {
2022-04-16 21:34:19 +03:00
n = n + "_"
}
2022-04-27 22:44:39 +03:00
return n
}
func (f Field) TypeLine() string {
n := f.FixedName()
2022-04-16 21:34:19 +03:00
m := ""
if f.Multiple {
m = "[]"
}
t := f.Type
if f.Type == "array" {
t = "[]*" + *f.ElementType
}
if f.Type == "map" {
t = "map[int]*" + *f.ElementType
}
if f.Type == "object" {
t = "*" + *f.ElementType
}
2022-04-18 14:39:28 +03:00
if f.Type == "enum" {
t = *f.ElementType
}
2022-05-04 15:07:35 +03:00
j := fmt.Sprintf("`json:\"%s\" legend:\"%s\" related:\"%s\"`", strcase.ToLowerCamel(f.Name), f.Legend, f.Related)
2022-04-16 21:34:19 +03:00
return fmt.Sprintf("%s %s%s %s", n, m, t, j)
}
2022-04-21 11:58:28 +03:00
func (f Field) MustInit() bool {
2022-04-29 16:28:08 +03:00
return f.Type == "map" || (f.Type == "int" && !f.Multiple) || strings.HasPrefix(f.Type, "map[")
2022-04-21 11:58:28 +03:00
}
func (f Field) Init() string {
2022-04-27 22:44:39 +03:00
n := f.FixedName()
2022-04-21 11:58:28 +03:00
if f.Type == "map" {
return fmt.Sprintf("%s: make(map[int]*%s),", n, *f.ElementType)
2022-04-29 16:28:08 +03:00
} else if strings.HasPrefix(f.Type, "map[") {
return fmt.Sprintf("%s: make(%s),", n, f.Type)
2022-04-21 11:58:28 +03:00
}
if f.Type == "int" && !f.Multiple {
return fmt.Sprintf("%s: -1,", n)
2022-04-16 21:34:19 +03:00
}
return ""
}
2022-04-19 21:32:20 +03:00
func (f Field) StartAction(obj Object, plus bool) string {
2022-04-27 22:44:39 +03:00
n := f.FixedName()
2022-04-16 21:34:19 +03:00
if f.Type == "object" {
2022-04-16 23:12:23 +03:00
var p string
if !plus {
2022-04-19 21:32:20 +03:00
p = fmt.Sprintf("v, _ := parse%s(p)", *f.ElementType)
2022-04-16 23:12:23 +03:00
} else {
2022-04-19 21:32:20 +03:00
p = fmt.Sprintf("v, _ := parse%sPlus(p, &%s{})", *f.ElementType, *f.ElementType)
2022-04-16 23:12:23 +03:00
}
2022-04-16 21:34:19 +03:00
if !f.Multiple {
return fmt.Sprintf("%s\nobj.%s = v", p, n)
} else {
return fmt.Sprintf("%s\nobj.%s = append(obj.%s, v)", p, n, n)
}
}
if f.Type == "array" || f.Type == "map" {
gen := fmt.Sprintf("parse%s", *f.ElementType)
if f.Type == "array" {
2022-05-04 10:26:26 +03:00
if !plus {
return fmt.Sprintf("parseArray(p, &obj.%s, %s)", f.Name, gen)
} else {
gen = fmt.Sprintf("parse%sPlus", *f.ElementType)
return fmt.Sprintf("parseArrayPlus(p, &obj.%s, %s)", f.Name, gen)
}
2022-04-16 21:34:19 +03:00
}
if f.Type == "map" {
if !plus {
2022-04-19 21:32:20 +03:00
return fmt.Sprintf("parseMap(p, &obj.%s, %s)", f.Name, gen)
2022-04-16 21:34:19 +03:00
} else {
gen = fmt.Sprintf("parse%sPlus", *f.ElementType)
2022-04-19 21:32:20 +03:00
return fmt.Sprintf("parseMapPlus(p, &obj.%s, %s)", f.Name, gen)
2022-04-16 21:34:19 +03:00
}
}
}
2022-04-18 14:39:28 +03:00
if f.Type == "int" || f.Type == "string" || f.Type == "bool" || f.Type == "enum" {
2022-04-27 22:44:39 +03:00
n := f.FixedName()
2022-04-19 21:32:20 +03:00
2022-04-27 22:44:39 +03:00
if f.Name == n {
2022-04-19 21:32:20 +03:00
n = f.CorrectedName(obj)
}
2022-05-06 16:40:45 +03:00
name2, ok := sameFields[obj.Name][strings.TrimSuffix(n, "_")]
if ok {
n = name2
}
2022-04-19 21:32:20 +03:00
s := "data, err := p.Value()\nif err != nil { return nil, err }\n"
if !f.Multiple {
if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = num(data)", s, n)
} else if f.Type == "string" {
2022-04-20 13:46:42 +03:00
return fmt.Sprintf("%sobj.%s = txt(data)", s, n)
2022-04-19 21:32:20 +03:00
} else if f.Type == "bool" {
s := "_, err := p.Value()\nif err != nil { return nil, err }\n"
return fmt.Sprintf("%sobj.%s = true", s, n)
} else if f.Type == "enum" {
2022-04-27 22:44:39 +03:00
return fmt.Sprintf("%sobj.%s = parse%s%s(txt(data))", s, n, obj.Name, f.CorrectedName(obj))
2022-04-19 21:32:20 +03:00
}
} else {
if f.Type == "int" {
return fmt.Sprintf("%sobj.%s = append(obj.%s, num(data))", s, n, n)
} else if f.Type == "string" {
2022-04-20 13:46:42 +03:00
return fmt.Sprintf("%sobj.%s = append(obj.%s, txt(data))", s, n, n)
2022-05-03 15:59:47 +03:00
} else if f.Type == "bool" {
s := "_, err := p.Value()\nif err != nil { return nil, err }\n"
return fmt.Sprintf("%sobj.%s = append(obj.%s, true)", s, n, n)
2022-04-19 21:32:20 +03:00
} else if f.Type == "enum" {
2022-04-27 22:44:39 +03:00
return fmt.Sprintf("%sobj.%s = append(obj.%s, parse%s%s(txt(data)))", s, n, n, obj.Name, f.CorrectedName(obj))
2022-04-19 21:32:20 +03:00
}
}
2022-04-16 21:34:19 +03:00
}
return ""
}
2022-04-18 11:36:29 +03:00
func (f Field) EndAction(obj Object) string {
2022-04-27 22:44:39 +03:00
n := f.FixedName()
2022-04-16 21:34:19 +03:00
2022-04-27 22:44:39 +03:00
if f.Name == n {
2022-04-18 11:36:29 +03:00
n = f.CorrectedName(obj)
2022-04-16 21:34:19 +03:00
}
if !f.Multiple {
if f.Type == "int" {
return fmt.Sprintf("obj.%s = n(data)", n)
} else if f.Type == "string" {
return fmt.Sprintf("obj.%s = string(data)", n)
} else if f.Type == "bool" {
return fmt.Sprintf("obj.%s = true", n)
2022-04-18 14:39:28 +03:00
} else if f.Type == "enum" {
2022-04-27 22:44:39 +03:00
return fmt.Sprintf("obj.%s = parse%s%s(string(data))", n, obj.Name, f.CorrectedName(obj))
2022-04-16 21:34:19 +03:00
}
} else {
if f.Type == "int" {
return fmt.Sprintf("obj.%s = append(obj.%s, n(data))", n, n)
} else if f.Type == "string" {
return fmt.Sprintf("obj.%s = append(obj.%s, string(data))", n, n)
2022-04-18 14:39:28 +03:00
} else if f.Type == "enum" {
2022-04-27 22:44:39 +03:00
return fmt.Sprintf("obj.%s = append(obj.%s, parse%s%s(string(data)))", n, n, obj.Name, f.CorrectedName(obj))
2022-04-16 21:34:19 +03:00
}
}
return ""
}
2022-04-16 23:12:23 +03:00
2022-05-05 17:55:57 +03:00
var entityRegex, _ = regexp.Compile("(civ|civ_id|enid|(^|[^d])entity(_id)?|^entity(_id)?|^source|^destination|^involved)(_?[0-9])?$")
2022-04-24 11:58:31 +03:00
var hfRegex, _ = regexp.Compile("(hfid|hf_id|hist_figure_id|histfig_id|histfig|bodies|_hf)")
2022-04-25 21:16:49 +03:00
var artifactRegex, _ = regexp.Compile("(item(_id)?|artifact_id)$")
2022-04-24 11:58:31 +03:00
var siteRegex, _ = regexp.Compile("(site_id|site)[0-9]?$")
2022-04-30 11:25:50 +03:00
var structureRegex, _ = regexp.Compile("(structure(_id)?)$")
2022-04-24 11:58:31 +03:00
var regionRegex, _ = regexp.Compile("(region_id|srid)$")
2022-05-03 22:39:00 +03:00
var worldConstructionRegex, _ = regexp.Compile("(wcid)$")
var writtenContentRegex, _ = regexp.Compile("^wc_id$")
2022-05-07 22:17:05 +03:00
var identityRegex, _ = regexp.Compile("identity")
2022-05-03 22:39:00 +03:00
var noRegex, _ = regexp.Compile("^XXXXX$")
2022-04-24 11:58:31 +03:00
func (obj Object) RelatedToEntity() string {
2022-05-03 22:39:00 +03:00
return obj.Related("entity", entityRegex, "")
2022-04-24 11:58:31 +03:00
}
func (obj Object) RelatedToHf() string {
2022-05-03 22:39:00 +03:00
return obj.Related("hf", hfRegex, "")
2022-04-24 11:58:31 +03:00
}
func (obj Object) RelatedToArtifact() string {
2022-05-03 22:39:00 +03:00
return obj.Related("artifact", artifactRegex, "")
2022-04-24 11:58:31 +03:00
}
func (obj Object) RelatedToSite() string {
2022-05-03 22:39:00 +03:00
return obj.Related("site", siteRegex, "")
2022-04-30 11:25:50 +03:00
}
func (obj Object) RelatedToStructure() string {
2022-05-03 22:39:00 +03:00
return obj.Related("structure", structureRegex, "x.RelatedToSite(siteId)")
2022-04-24 11:58:31 +03:00
}
func (obj Object) RelatedToRegion() string {
2022-05-03 22:39:00 +03:00
return obj.Related("region", regionRegex, "")
}
func (obj Object) RelatedToWorldConstruction() string {
return obj.Related("worldConstruction", worldConstructionRegex, "")
}
func (obj Object) RelatedToWrittenContent() string {
return obj.Related("writtenContent", writtenContentRegex, "")
}
func (obj Object) RelatedToDanceForm() string {
return obj.Related("danceForm", noRegex, "")
}
func (obj Object) RelatedToMusicalForm() string {
return obj.Related("musicalForm", noRegex, "")
}
func (obj Object) RelatedToPoeticForm() string {
return obj.Related("poeticForm", noRegex, "")
2022-04-24 11:58:31 +03:00
}
2022-05-04 15:07:35 +03:00
func (obj Object) RelatedToMountain() string {
return obj.Related("mountain", noRegex, "")
}
2022-05-07 22:17:05 +03:00
func (obj Object) RelatedToIdentity() string {
return obj.Related("identity", identityRegex, "")
}
2022-04-24 11:58:31 +03:00
2022-05-03 22:39:00 +03:00
func (obj Object) Related(relation string, regex *regexp.Regexp, init string) string {
2022-04-16 23:12:23 +03:00
var list []string
for n, f := range obj.Fields {
2022-05-06 16:18:43 +03:00
if f.Type == "int" && !f.SameField(obj) && (relation == f.Related || (f.Related == "" && regex.MatchString(n))) {
2022-04-18 11:36:29 +03:00
if !f.Multiple {
list = append(list, fmt.Sprintf("x.%s == id", f.Name))
} else {
list = append(list, fmt.Sprintf("containsInt(x.%s, id)", f.Name))
}
2022-04-16 23:12:23 +03:00
}
}
2022-05-04 15:07:35 +03:00
for _, f := range obj.Additional {
if f.Type == "int" && relation == f.Related {
list = append(list, fmt.Sprintf("x.%s == id", f.Name))
} else if f.Type == "[]int" && relation == f.Related {
list = append(list, fmt.Sprintf("containsInt(x.%s, id)", f.Name))
}
}
2022-04-19 14:25:04 +03:00
sort.Strings(list)
2022-04-16 23:12:23 +03:00
if len(list) > 0 {
2022-04-30 11:25:50 +03:00
l := strings.Join(list, " || ")
if init == "" {
return l
} else {
return init + "&& (" + l + ")"
}
2022-04-16 23:12:23 +03:00
}
return "false"
}
2022-04-18 11:36:29 +03:00
func (obj Object) LegendFields(t string) []Field {
var list []Field
for _, f := range obj.Fields {
if f.Name != "Name" && f.Name != "Id" && f.Name != "Type" && f.Legend == t && f.Type != "object" && !f.Multiple {
list = append(list, f)
}
}
2022-04-19 14:25:04 +03:00
sort.Slice(list, func(i, j int) bool { return list[i].Name < list[j].Name })
2022-04-18 11:36:29 +03:00
return list
}
2022-04-21 11:58:28 +03:00
func (f Field) JsonMarshal() string {
2022-04-27 22:44:39 +03:00
n := f.FixedName()
2022-04-21 11:58:28 +03:00
if f.Type == "int" && !f.Multiple {
return fmt.Sprintf(`if x.%s != -1 { d["%s"] = x.%s }`, n, strcase.ToLowerCamel(f.Name), n)
}
if f.Type == "enum" && !f.Multiple {
return fmt.Sprintf(`if x.%s != 0 { d["%s"] = x.%s }`, n, strcase.ToLowerCamel(f.Name), n)
}
return fmt.Sprintf(`d["%s"] = x.%s`, strcase.ToLowerCamel(f.Name), n)
}
2022-04-18 11:36:29 +03:00
func (f Field) SameField(obj Object) bool {
if f.Legend != "plus" {
return false
}
_, ok := sameFields[obj.Name][f.Name]
// fmt.Println(obj.Name, f.Name, ok)
return ok
}
func (f Field) CorrectedName(obj Object) string {
if f.Legend != "plus" {
2022-04-30 11:25:50 +03:00
if f.Name == "LocalId" && obj.Id {
return "Id_"
}
2022-04-18 11:36:29 +03:00
return f.Name
}
2022-05-06 16:40:45 +03:00
n, ok := sameFields[obj.Name][strings.TrimSuffix(f.Name, "_")]
2022-04-18 11:36:29 +03:00
if ok {
return n
}
return f.Name
}
2022-04-18 14:39:28 +03:00
func (f Field) UniqueEnumValues() []string {
vs := make(map[string]bool)
for _, k := range *f.EnumValues {
vs[enumValue(k)] = true
}
list := util.Keys(vs)
sort.Strings(list)
return list
}