Browse Source

♻️ variable to their values & refactor validate

master
n0m1s 6 years ago
parent
commit
d686b1b6b4
Signed by: nomis GPG Key ID: BC0454CAD76FE803
2 changed files with 49 additions and 22 deletions
  1. +23
    -10
      jdr/sheet.go
  2. +26
    -12
      jdr/type.go

+ 23
- 10
jdr/sheet.go View File

@ -12,17 +12,26 @@ type Page struct {
Items []SheetItem
}
type SheetItem interface{
type SheetItem interface {
Name() string
}
type Block struct {
Name string
name string
Items []SheetItem
}
func (self *Block) Name() string {
return self.name
}
type Variable struct {
Type VariableType
Name string
name string
}
func (self *Variable) Name() string {
return self.name
}
type CharacterSheet struct {
@ -99,7 +108,7 @@ func parseVariable(root xmlNode, sheet *CharacterSheet, defaults *Variable) (*Va
ret.Type = t
typeOk = true
case "name":
ret.Name = attrVal
ret.name = attrVal
nameOk = true
}
}
@ -108,7 +117,7 @@ func parseVariable(root xmlNode, sheet *CharacterSheet, defaults *Variable) (*Va
return nil, errors.New("Unnamed variable")
}
if !typeOk {
return nil, errors.New("No type provided for variable \"" + ret.Name + "\"")
return nil, errors.New("No type provided for variable \"" + ret.Name() + "\"")
}
return ret, nil
@ -123,17 +132,19 @@ func parseBlock(root xmlNode, sheet *CharacterSheet) (*Block, error) {
switch attrName {
case "name":
ret.Name = attrVal
ret.name = attrVal
}
}
for i := 0; i < len(root.Nodes); i++ {
item, err := parseItem(root.Nodes[i], sheet)
items, err := parseItem(root.Nodes[i], sheet)
if err != nil {
return nil, err
}
ret.Items = append(ret.Items, item)
for _, item := range(items) {
ret.Items = append(ret.Items, item)
}
}
return ret, nil
@ -233,12 +244,14 @@ func parsePage(root xmlNode, sheet *CharacterSheet) (*Page, error) {
ret := &Page{}
for i := 0; i < len(root.Nodes); i++ {
item, err := parseItem(root.Nodes[i], sheet)
items, err := parseItem(root.Nodes[i], sheet)
if err != nil {
return nil, err
}
ret.Items = append(ret.Items, item)
for _, item := range(items) {
ret.Items = append(ret.Items, item)
}
}
return ret, nil


+ 26
- 12
jdr/type.go View File

@ -4,12 +4,11 @@ import(
"log"
"errors"
"strings"
"strconv"
"regexp"
)
var regexp_int = regexp.MustCompile(`^[0-9]+$`)
var regexp_float = regexp.MustCompile(`^[+-]?[0-9]*\.[0-9]+$`)
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)$`)
type VariableType interface {
Name() string
@ -32,25 +31,35 @@ func (*VariableType_bool) Name() string {
return "bool"
}
func (*VariableType_bool) Validate(s string) bool {
switch strings.ToLower(s) {
func (*VariableType_bool) ToBool(value string) (bool, error) {
switch strings.ToLower(value) {
case "true":
return true
return true, nil
case "false":
return true
return false, nil
default:
return false
return false, errors.New("unknown bool value")
}
}
func (self *VariableType_bool) Validate(value string) bool {
_, err := self.ToBool(value)
return err == nil
}
type VariableType_int struct {}
func (*VariableType_int) Name() string {
return "int"
}
func (*VariableType_int) Validate(s string) bool {
return regexp_int.MatchString(s)
func (self *VariableType_int) ToInt(value string) (int64, error) {
return strconv.ParseInt(value, 10, 64)
}
func (self *VariableType_int) Validate(value string) bool {
_, err := self.ToInt(value)
return err == nil
}
type VariableType_float struct {}
@ -59,8 +68,13 @@ func (*VariableType_float) Name() string {
return "float"
}
func (*VariableType_float) Validate(s string) bool {
return regexp_float.MatchString(s)
func (*VariableType_float) ToFloat(value string) (float64, error) {
return strconv.ParseFloat(value, 64)
}
func (self *VariableType_float) Validate(value string) bool {
_, err := self.ToFloat(value)
return err == nil
}
type VariableType_string struct {}


Loading…
Cancel
Save