-
Notifications
You must be signed in to change notification settings - Fork 18
/
qualified_name.go
58 lines (50 loc) · 1.44 KB
/
qualified_name.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
// Copyright 2021 Converter Systems LLC. All rights reserved.
package ua
import (
"fmt"
"strconv"
"strings"
)
// QualifiedName pairs a name and a namespace index.
type QualifiedName struct {
NamespaceIndex uint16
Name string
}
// NewQualifiedName constructs a QualifiedName from a namespace index and a name.
func NewQualifiedName(ns uint16, text string) QualifiedName {
return QualifiedName{ns, text}
}
// ParseQualifiedName returns a QualifiedName from a string, e.g. ParseQualifiedName("2:Demo")
func ParseQualifiedName(s string) QualifiedName {
var ns uint64
var pos = strings.Index(s, ":")
if pos == -1 {
return QualifiedName{uint16(ns), s}
}
ns, err := strconv.ParseUint(s[:pos], 10, 16)
if err != nil {
return QualifiedName{uint16(ns), s}
}
s = s[pos+1:]
return QualifiedName{uint16(ns), s}
}
// ParseBrowsePath returns a slice of QualifiedNames from a string, e.g. ParseBrowsePath("2:Demo/2:Dynamic")
func ParseBrowsePath(s string) []QualifiedName {
//TODO: see part4 Annex A.2
if len(s) == 0 {
return []QualifiedName{}
}
toks := strings.Split(s, "/")
path := make([]QualifiedName, len(toks))
for i, tok := range toks {
path[i] = ParseQualifiedName(tok)
}
return path
}
// String returns a string representation, e.g. "2:Demo"
func (a QualifiedName) String() string {
return fmt.Sprintf("%d:%s", a.NamespaceIndex, a.Name)
}
func (a QualifiedName) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}