vikunja-api/vendor/github.com/laurent22/ical-go/node.go

186 lines
3.8 KiB
Go
Raw Permalink Normal View History

2019-05-22 19:48:48 +02:00
package ical
import (
"regexp"
"strconv"
2019-05-24 19:45:33 +02:00
"time"
2019-05-22 19:48:48 +02:00
)
type Node struct {
2019-05-24 19:45:33 +02:00
Name string
Value string
Type int // 1 = Object, 0 = Name/Value
2019-05-22 19:48:48 +02:00
Parameters map[string]string
2019-05-24 19:45:33 +02:00
Children []*Node
2019-05-22 19:48:48 +02:00
}
func (this *Node) ChildrenByName(name string) []*Node {
var output []*Node
for _, child := range this.Children {
if child.Name == name {
output = append(output, child)
}
}
return output
}
func (this *Node) ChildByName(name string) *Node {
for _, child := range this.Children {
if child.Name == name {
return child
}
}
return nil
}
func (this *Node) PropString(name string, defaultValue string) string {
for _, child := range this.Children {
if child.Name == name {
return child.Value
}
}
return defaultValue
}
func (this *Node) PropDate(name string, defaultValue time.Time) time.Time {
node := this.ChildByName(name)
2019-05-24 19:45:33 +02:00
if node == nil {
return defaultValue
}
2019-05-22 19:48:48 +02:00
tzid := node.Parameter("TZID", "")
2019-05-24 19:45:33 +02:00
allDay := node.Parameter("VALUE", "") == "DATE"
2019-05-22 19:48:48 +02:00
var output time.Time
var err error
if tzid != "" {
loc, err := time.LoadLocation(tzid)
2019-05-24 19:45:33 +02:00
if err != nil {
panic(err)
}
2019-05-22 19:48:48 +02:00
output, err = time.ParseInLocation("20060102T150405", node.Value, loc)
2019-05-24 19:45:33 +02:00
} else if allDay {
output, err = time.Parse("20060102", node.Value)
2019-05-22 19:48:48 +02:00
} else {
output, err = time.Parse("20060102T150405Z", node.Value)
}
2019-05-24 19:45:33 +02:00
if err != nil {
panic(err)
}
2019-05-22 19:48:48 +02:00
return output
}
func (this *Node) PropDuration(name string) time.Duration {
durStr := this.PropString(name, "")
if durStr == "" {
return time.Duration(0)
}
durRgx := regexp.MustCompile("PT(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?")
matches := durRgx.FindStringSubmatch(durStr)
if len(matches) != 4 {
return time.Duration(0)
}
strToDuration := func(value string) time.Duration {
d := 0
if value != "" {
d, _ = strconv.Atoi(value)
}
return time.Duration(d)
}
hours := strToDuration(matches[1])
min := strToDuration(matches[2])
sec := strToDuration(matches[3])
2019-05-24 19:45:33 +02:00
return hours*time.Hour + min*time.Minute + sec*time.Second
2019-05-22 19:48:48 +02:00
}
func (this *Node) PropInt(name string, defaultValue int) int {
n := this.PropString(name, "")
2019-05-24 19:45:33 +02:00
if n == "" {
return defaultValue
}
2019-05-22 19:48:48 +02:00
output, err := strconv.Atoi(n)
2019-05-24 19:45:33 +02:00
if err != nil {
panic(err)
}
2019-05-22 19:48:48 +02:00
return output
}
2019-05-24 19:45:33 +02:00
func (this *Node) DigProperty(propPath ...string) (string, bool) {
2019-05-22 19:48:48 +02:00
return this.dig("prop", propPath...)
}
func (this *Node) Parameter(name string, defaultValue string) string {
2019-05-24 19:45:33 +02:00
if len(this.Parameters) <= 0 {
return defaultValue
}
2019-05-22 19:48:48 +02:00
v, ok := this.Parameters[name]
2019-05-24 19:45:33 +02:00
if !ok {
return defaultValue
}
2019-05-22 19:48:48 +02:00
return v
}
2019-05-24 19:45:33 +02:00
func (this *Node) DigParameter(paramPath ...string) (string, bool) {
2019-05-22 19:48:48 +02:00
return this.dig("param", paramPath...)
}
// Digs a value based on a given value path.
// valueType: can be "param" or "prop".
// valuePath: the path to access the value.
// Returns ("", false) when not found or (value, true) when found.
//
// Example:
// dig("param", "VCALENDAR", "VEVENT", "DTEND", "TYPE") -> It will search for "VCALENDAR" node,
// then a "VEVENT" node, then a "DTEND" note, then finally the "TYPE" param.
2019-05-24 19:45:33 +02:00
func (this *Node) dig(valueType string, valuePath ...string) (string, bool) {
2019-05-22 19:48:48 +02:00
current := this
lastIndex := len(valuePath) - 1
for _, v := range valuePath[:lastIndex] {
current = current.ChildByName(v)
if current == nil {
return "", false
}
}
target := valuePath[lastIndex]
value := ""
if valueType == "param" {
value = current.Parameter(target, "")
} else if valueType == "prop" {
value = current.PropString(target, "")
}
if value == "" {
return "", false
}
return value, true
}
func (this *Node) String() string {
s := ""
if this.Type == 1 {
s += "===== " + this.Name
s += "\n"
} else {
s += this.Name
s += ":" + this.Value
s += "\n"
}
for _, child := range this.Children {
s += child.String()
}
if this.Type == 1 {
s += "===== /" + this.Name
s += "\n"
}
return s
}