From 47e8c2f4884da925946489958b501e29866c2643 Mon Sep 17 00:00:00 2001 From: n0m1s Date: Sun, 4 Aug 2019 23:47:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Custom=20struct=20ToHTML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdr/sheet.go | 5 +-- jdr/type.go | 102 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/jdr/sheet.go b/jdr/sheet.go index 8460c31..2d6a60e 100644 --- a/jdr/sheet.go +++ b/jdr/sheet.go @@ -323,13 +323,10 @@ func parseSheet(root xmlNode) (*CharacterSheet, error) { switch node.XMLName.Local { case "custom-types": - types, err := parseCustomTypes(node) + err := parseCustomTypes(node, sheet) if err != nil { return nil, err } - for j := 0; j < len(types); j++ { - sheet.AddType(types[j]) - } case "page": page, err := parsePage(node, sheet) diff --git a/jdr/type.go b/jdr/type.go index e62b60d..58ddf80 100644 --- a/jdr/type.go +++ b/jdr/type.go @@ -8,6 +8,7 @@ import( "strings" "strconv" "regexp" + "encoding/json" ) var regexp_d = regexp.MustCompile(`^[1-9][0-9]*d([468]|1[02]|20|100)$`) @@ -258,7 +259,7 @@ func (self *CustomEnum) DefaultValue() string { type CustomStruct struct { name string - Variables []Variable + Variables []*Variable } func (self *CustomStruct) Name() string { @@ -266,19 +267,67 @@ func (self *CustomStruct) Name() string { } func (self *CustomStruct) Validate(s string) bool { - //FIXME - log.Println("Custom struct Validate is not (yet) implemented") - return false + var jsonValue map[string]interface{} + err := json.Unmarshal([]byte(s), &jsonValue) + if err != nil { + log.Fatal(err) + return false + } + + for _, v := range self.Variables { + jsonVal, isString := jsonValue[v.Name()].(string) + if !isString || ! v.Type.Validate(jsonVal) { + return false + } + } + + return true } func (self *CustomStruct) ToHTML(value string) (template.HTML, error) { - //FIXME: implement - return template.HTML(""), nil + + var jsonValue map[string]interface{} + err := json.Unmarshal([]byte(value), &jsonValue) + if err != nil { + log.Fatal(err) + return template.HTML(""), errors.New("Cannot unmarshal JSON") + } + + ret := template.HTML("") + ret += `
` + for _, v := range self.Variables { + log.Println("variable:", v.Name()) + jsonVal, isString := jsonValue[v.Name()].(string) + if !isString { + return template.HTML(""), errors.New("Variable " + v.Name() + " is not a string") + } + + html, err := v.Type.ToHTML(jsonVal) + if err != nil { + return template.HTML(""), err + } + + ret += html + } + ret += `
` + + return ret, nil } func (self *CustomStruct) DefaultValue() string { - //FIXME: non-generic version - return "" + ret := `{` + for i, v := range self.Variables { + if i > 0 { + ret += "," + } + ret += `"` + ret += v.Name() + ret += `":"` + ret += v.Type.DefaultValue() + ret += `"` + } + ret += `}` + return ret } func parseCustomEnum(root xmlNode) (*CustomEnum, error) { @@ -329,7 +378,7 @@ func parseCustomEnum(root xmlNode) (*CustomEnum, error) { return &customEnum, nil } -func parseCustomStruct(root xmlNode) (*CustomStruct, error) { +func parseCustomStruct(root xmlNode, sheet *CharacterSheet) (*CustomStruct, error) { customType := CustomStruct{} nameOk := false @@ -343,16 +392,31 @@ func parseCustomStruct(root xmlNode) (*CustomStruct, error) { customType.name = attrVal } } + if !nameOk { return nil, errors.New("Unnamed custom type") } + for i := 0; i < len(root.Nodes); i++ { + node := root.Nodes[i] + + switch node.XMLName.Local { + case "variable": + variable, err := parseVariable(node, sheet, nil) + if err != nil { + return nil, err + } + + customType.Variables = append(customType.Variables, variable) + default: + return nil, errors.New("Unknown tag in custom type: " + node.XMLName.Local) + } + } + return &customType, nil } -func parseCustomTypes(root xmlNode) ([]VariableType, error) { - types := []VariableType{} - +func parseCustomTypes(root xmlNode, sheet *CharacterSheet) (error) { for i := 0; i < len(root.Nodes); i++ { node := root.Nodes[i] @@ -360,19 +424,19 @@ func parseCustomTypes(root xmlNode) ([]VariableType, error) { case "enum": customEnum, err := parseCustomEnum(node) if err != nil { - return []VariableType{}, err + return err } - types = append(types, customEnum) + sheet.AddType(customEnum) case "type": - customType, err := parseCustomStruct(node) + customType, err := parseCustomStruct(node, sheet) if err != nil { - return []VariableType{}, err + return err } - types = append(types, customType) + sheet.AddType(customType) default: - return []VariableType{}, errors.New("Unknown CustomType: " + root.Nodes[i].XMLName.Local) + return errors.New("Unknown CustomType: " + root.Nodes[i].XMLName.Local) } } - return types, nil + return nil }