2021-09-04 12:16:14 +00:00
|
|
|
package server
|
|
|
|
|
2021-09-11 09:58:27 +00:00
|
|
|
import "strings"
|
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
// Label keeps one piece of information about a single server
|
|
|
|
type Label string
|
|
|
|
|
|
|
|
func (l Label) String() string {
|
|
|
|
return string(l)
|
|
|
|
}
|
|
|
|
|
2021-09-11 09:58:27 +00:00
|
|
|
// GetPart returns specific part of the label, if part index is higher than last available index it returns empty string.
|
|
|
|
func (l Label) GetPart(idx int) string {
|
|
|
|
parts := strings.Split(l.String(), ":")
|
|
|
|
|
|
|
|
if idx < 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(parts) >= idx {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPart1 is exactly same as GetPart but it splits the label only once, this is good for IPv6 addresses
|
|
|
|
func (l Label) GetPart1(idx int) string {
|
|
|
|
parts := strings.SplitN(l.String(), ":", 2)
|
|
|
|
|
|
|
|
if idx < 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(parts) >= idx {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[idx]
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
// Labels stores multiple Label records
|
|
|
|
type Labels []Label
|
2021-09-04 23:27:39 +00:00
|
|
|
|
|
|
|
// StringSlice return slice of Label as strings
|
|
|
|
func (l *Labels) StringSlice() []string {
|
|
|
|
labelsString := []string{}
|
|
|
|
|
|
|
|
for _, label := range *l {
|
|
|
|
labelsString = append(labelsString, label.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return labelsString
|
|
|
|
}
|