-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord_route.go
127 lines (108 loc) · 2.66 KB
/
record_route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"errors"
"fmt"
"strings"
)
type RecordRoute struct {
recRoute []*RecRoute
}
type RecRoute struct {
nameAddr *NameAddr
rrParam []KeyValue
}
func NewRecordRoute() *RecordRoute {
return &RecordRoute{recRoute: make([]*RecRoute, 0)}
}
func ParseRecordRoute(s string) (*RecordRoute, error) {
rr := NewRecordRoute()
for _, t := range strings.Split(s, ",") {
recRoute, err := ParseRecRoute(t)
if err != nil {
return nil, err
}
rr.AddRecRoute(recRoute)
}
if len(rr.recRoute) > 0 {
return rr, nil
} else {
return nil, errors.New("empty Record-Route")
}
}
func ParseRecRoute(s string) (*RecRoute, error) {
recRoute := &RecRoute{nameAddr: nil, rrParam: make([]KeyValue, 0)}
pos := strings.Index(s, ">")
if pos == -1 {
return nil, errors.New("invalid syntax of rec-route")
}
nameAddr, err := ParseNameAddr(s[0 : pos+1])
if err != nil {
return nil, err
}
recRoute.nameAddr = nameAddr
s = strings.TrimSpace(s[pos+1:])
if len(s) <= 0 {
return recRoute, nil
}
if s[0] != ';' {
return nil, errors.New("invalid rec-route syntax")
}
s = s[1:]
for _, t := range strings.Split(s, ";") {
param, err := ParseGenericParam(t)
if err != nil {
return nil, err
}
recRoute.rrParam = append(recRoute.rrParam, param)
}
return recRoute, nil
}
func (r *RecordRoute) GetRecRouteCount() int {
return len(r.recRoute)
}
func (r *RecordRoute) GetRecRoute(index int) (*RecRoute, error) {
if index < 0 || index >= len(r.recRoute) {
return nil, fmt.Errorf("index %d is out of bound", index)
}
return r.recRoute[index], nil
}
func (r *RecordRoute) AddRecRoute(recRoute *RecRoute) {
r.recRoute = append(r.recRoute, recRoute)
}
func (r *RecordRoute) String() string {
buf := bytes.NewBuffer(make([]byte, 0))
for index, recRoute := range r.recRoute {
if index != 0 {
fmt.Fprintf(buf, ",")
}
fmt.Fprintf(buf, "%v", recRoute)
}
return buf.String()
}
func NewRecRoute(nameAddr *NameAddr) *RecRoute {
return &RecRoute{nameAddr: nameAddr, rrParam: make([]KeyValue, 0)}
}
func (r *RecRoute) GetNameAddr() *NameAddr {
return r.nameAddr
}
func (r *RecRoute) AddParam(name string, value string) {
r.rrParam = append(r.rrParam, KeyValue{Key: name, Value: value})
}
func (r *RecRoute) GetParamCount() int {
return len(r.rrParam)
}
func (r *RecRoute) GetParam(index int) (KeyValue, error) {
if index < 0 || index >= len(r.rrParam) {
return KeyValue{Key: "", Value: ""}, errors.New("out of bound")
}
return r.rrParam[index], nil
}
func (r *RecRoute) String() string {
buf := bytes.NewBuffer(make([]byte, 0))
fmt.Fprintf(buf, "%v", r.nameAddr)
for _, param := range r.rrParam {
fmt.Fprintf(buf, ";%v", param)
}
return buf.String()
}