forked from blind-oracle/go-radius
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dictionary_helpers.go
73 lines (65 loc) · 1.61 KB
/
dictionary_helpers.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
package radius
import (
"strings"
)
// DictionaryAttr structure for mass Attribute import
type DictionaryAttr struct {
Type byte
Name string
Tagged bool
Codec AttributeCodec
}
// VendorDict Vendor dictionary for configs
type VendorDict struct {
VendorName string
VendorID uint32
Attrs []struct {
Name string
Type byte
Tagged bool
RadType string
Aliases []string
}
}
// DictionaryContainer additional dictionaries
type DictionaryContainer interface {
Dict() (vendorID uint32, attrs []DictionaryAttr)
}
// RegisterDC register attributes from DictionaryContainer
func (d *Dictionary) RegisterDC(dict DictionaryContainer) error {
vendorID, nAttr := dict.Dict()
for _, a := range nAttr {
if err := d.VsaRegisterTagFlag(vendorID, a.Name, a.Type, a.Tagged, a.Codec); err != nil {
return err
}
}
return nil
}
// MustRegisterDC is a helper for RegisterDC that panics if it returns an error.
func (d *Dictionary) MustRegisterDC(dict DictionaryContainer) {
if err := d.RegisterDC(dict); err != nil {
panic(err)
}
}
// StrsToAttrs makes []*Attribute from map[string]string
// this suitable for reply from sql backend etc...
// returns last error
func (d *Dictionary) StrsToAttrs(m map[string]string) (attrs []*Attribute, err error) {
attrs = []*Attribute{}
var a *Attribute
var lastErr error
for name, val := range m {
if idx := strings.Index(name, "."); idx > 0 {
name = name[:idx]
}
if a, lastErr = d.Attr(name, val); lastErr == nil {
// log.Printf("Error:%v (%v:%v)", err, name, val)
// return
attrs = append(attrs, a)
} else {
err = lastErr
continue
}
}
return
}