Debian Codegraph @

component/identifiable.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package component // import "go.opentelemetry.io/collector/component"
5
6 import (
7 "encoding"
8 "errors"
9 "fmt"
10 "regexp"
11 "strings"
12 )
13
14 // typeAndNameSeparator is the separator that is used between type and name in type/name composite keys.
15 const typeAndNameSeparator = "/"
16
17 var (
18 // typeRegexp is used to validate the type of component.
19 // A type must start with an ASCII alphabetic character and
20 // can only contain ASCII alphanumeric characters and '_'.
21 // This must be kept in sync with the regex in cmd/mdatagen/validate.go.
22 typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]{0,62}$`)
23
24 // nameRegexp is used to validate the name of a component. A name can consist of
25 // 1 to 1024 Unicode characters excluding whitespace, control characters, and
26 // symbols.
27 nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]+$`)
28 )
29
30 var _ fmt.Stringer = Type{}
31
32 // Type is the component type as it is used in the config.
33 type Type struct {
34 name string
35 }
36
37 // String returns the string representation of the type.
38 func (t Type) String() string {
39 return t.name
40 }
41
42 // MarshalText marshals returns the Type name.
43 func (t Type) MarshalText() ([]byte, error) {
44 return []byte(t.name), nil
45 }
46
47 // NewType creates a type. It returns an error if the type is invalid.
48 // A type must
49 // - have at least one character,
50 // - start with an ASCII alphabetic character and
51 // - can only contain ASCII alphanumeric characters and '_'.
52 func NewType(ty string) (Type, error) {
53 if ty == "" {
54 return Type{}, errors.New("id must not be empty")
55 }
56 if !typeRegexp.MatchString(ty) {
57 return Type{}, fmt.Errorf("invalid character(s) in type %q", ty)
58 }
59 return Type{name: ty}, nil
60 }
61
62 // MustNewType creates a type. It panics if the type is invalid.
63 // A type must
64 // - have at least one character,
65 // - start with an ASCII alphabetic character and
66 // - can only contain ASCII alphanumeric characters and '_'.
67 func MustNewType(strType string) Type {
68 ty, err := NewType(strType)
69 if err != nil {
70 panic(err)
71 }
72 return ty
73 }
74
75 var (
76 _ fmt.Stringer = ID{}
77 _ encoding.TextMarshaler = ID{}
78 _ encoding.TextUnmarshaler = (*ID)(nil)
79 )
80
81 // ID represents the identity for a component. It combines two values:
82 // * type - the Type of the component.
83 // * name - the name of that component.
84 // The component ID (combination type + name) is unique for a given component.Kind.
85 type ID struct {
86 typeVal Type `mapstructure:"-"`
87 nameVal string `mapstructure:"-"`
88 }
89
90 // NewID returns a new ID with the given Type and empty name.
91 func NewID(typeVal Type) ID {
92 return ID{typeVal: typeVal}
93 }
94
95 // MustNewID builds a Type and returns a new ID with the given Type and empty name.
96 // This is equivalent to NewID(MustNewType(typeVal)).
97 // See MustNewType to check the valid values of typeVal.
98 func MustNewID(typeVal string) ID {
99 return NewID(MustNewType(typeVal))
100 }
101
102 // NewIDWithName returns a new ID with the given Type and name.
103 func NewIDWithName(typeVal Type, nameVal string) ID {
104 return ID{typeVal: typeVal, nameVal: nameVal}
105 }
106
107 // MustNewIDWithName builds a Type and returns a new ID with the given Type and name.
108 // This is equivalent to NewIDWithName(MustNewType(typeVal), nameVal).
109 // See MustNewType to check the valid values of typeVal.
110 func MustNewIDWithName(typeVal, nameVal string) ID {
111 return NewIDWithName(MustNewType(typeVal), nameVal)
112 }
113
114 // Type returns the type of the component.
115 func (id ID) Type() Type {
116 return id.typeVal
117 }
118
119 // Name returns the custom name of the component.
120 func (id ID) Name() string {
121 return id.nameVal
122 }
123
124 // MarshalText implements the encoding.TextMarshaler interface.
125 // This marshals the type and name as one string in the config.
126 func (id ID) MarshalText() ([]byte, error) {
127 return []byte(id.String()), nil
128 }
129
130 // UnmarshalText implements the encoding.TextUnmarshaler interface.
131 func (id *ID) UnmarshalText(text []byte) error {
132 idStr := string(text)
133 typeStr, nameStr, hasName := strings.Cut(idStr, typeAndNameSeparator)
134 typeStr = strings.TrimSpace(typeStr)
135
136 if typeStr == "" {
137 if hasName {
138 return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
139 }
140 return errors.New("id must not be empty")
141 }
142
143 if hasName {
144 // "name" part is present.
145 nameStr = strings.TrimSpace(nameStr)
146 if nameStr == "" {
147 return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator)
148 }
149 if err := validateName(nameStr); err != nil {
150 return fmt.Errorf("in %q id: %w", nameStr, err)
151 }
152 }
153
154 var err error
155 if id.typeVal, err = NewType(typeStr); err != nil {
156 return fmt.Errorf("in %q id: %w", idStr, err)
157 }
158 id.nameVal = nameStr
159
160 return nil
161 }
162
163 // String returns the ID string representation as "type[/name]" format.
164 func (id ID) String() string {
165 if id.nameVal == "" {
166 return id.typeVal.String()
167 }
168
169 return id.typeVal.String() + typeAndNameSeparator + id.nameVal
170 }
171
172 func validateName(nameStr string) error {
173 if len(nameStr) > 1024 {
174 return fmt.Errorf("name %q is longer than 1024 characters (%d characters)", nameStr, len(nameStr))
175 }
176 if !nameRegexp.MatchString(nameStr) {
177 return fmt.Errorf("invalid character(s) in name %q", nameStr)
178 }
179 return nil
180 }