Browse Source

Custom struct ToHTML

master
n0m1s 6 years ago
parent
commit
47e8c2f488
Signed by: nomis GPG Key ID: BC0454CAD76FE803
2 changed files with 84 additions and 23 deletions
  1. +1
    -4
      jdr/sheet.go
  2. +83
    -19
      jdr/type.go

+ 1
- 4
jdr/sheet.go View File

@ -323,13 +323,10 @@ func parseSheet(root xmlNode) (*CharacterSheet, error) {
switch node.XMLName.Local { switch node.XMLName.Local {
case "custom-types": case "custom-types":
types, err := parseCustomTypes(node)
err := parseCustomTypes(node, sheet)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for j := 0; j < len(types); j++ {
sheet.AddType(types[j])
}
case "page": case "page":
page, err := parsePage(node, sheet) page, err := parsePage(node, sheet)


+ 83
- 19
jdr/type.go View File

@ -8,6 +8,7 @@ import(
"strings" "strings"
"strconv" "strconv"
"regexp" "regexp"
"encoding/json"
) )
var regexp_d = regexp.MustCompile(`^[1-9][0-9]*d([468]|1[02]|20|100)$`) 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 { type CustomStruct struct {
name string name string
Variables []Variable
Variables []*Variable
} }
func (self *CustomStruct) Name() string { func (self *CustomStruct) Name() string {
@ -266,19 +267,67 @@ func (self *CustomStruct) Name() string {
} }
func (self *CustomStruct) Validate(s string) bool { 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) { 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 += `<div>`
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 += `</div>`
return ret, nil
} }
func (self *CustomStruct) DefaultValue() string { 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) { func parseCustomEnum(root xmlNode) (*CustomEnum, error) {
@ -329,7 +378,7 @@ func parseCustomEnum(root xmlNode) (*CustomEnum, error) {
return &customEnum, nil return &customEnum, nil
} }
func parseCustomStruct(root xmlNode) (*CustomStruct, error) {
func parseCustomStruct(root xmlNode, sheet *CharacterSheet) (*CustomStruct, error) {
customType := CustomStruct{} customType := CustomStruct{}
nameOk := false nameOk := false
@ -343,16 +392,31 @@ func parseCustomStruct(root xmlNode) (*CustomStruct, error) {
customType.name = attrVal customType.name = attrVal
} }
} }
if !nameOk { if !nameOk {
return nil, errors.New("Unnamed custom type") 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 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++ { for i := 0; i < len(root.Nodes); i++ {
node := root.Nodes[i] node := root.Nodes[i]
@ -360,19 +424,19 @@ func parseCustomTypes(root xmlNode) ([]VariableType, error) {
case "enum": case "enum":
customEnum, err := parseCustomEnum(node) customEnum, err := parseCustomEnum(node)
if err != nil { if err != nil {
return []VariableType{}, err
return err
} }
types = append(types, customEnum)
sheet.AddType(customEnum)
case "type": case "type":
customType, err := parseCustomStruct(node)
customType, err := parseCustomStruct(node, sheet)
if err != nil { if err != nil {
return []VariableType{}, err
return err
} }
types = append(types, customType)
sheet.AddType(customType)
default: 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
} }

Loading…
Cancel
Save