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
|
package app
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib/encoding"
"github.com/mesos/mesos-go/api/v1/lib/encoding/codecs"
)
var (
errZeroLengthLabelKey = errors.New("zero-length label key")
)
type URL struct{ url.URL }
func (u *URL) Set(value string) error {
parsed, err := url.Parse(value)
if err != nil {
return err
}
u.URL = *parsed
return nil
}
type codec struct{ encoding.Codec }
func (c *codec) Set(value string) error {
v := strings.ToLower(value)
for _, codec := range codecs.ByMediaType {
if v == codec.Name {
c.Codec = codec
return nil
}
}
return fmt.Errorf("bad codec %q", value)
}
type Labels []mesos.Label
func (labels *Labels) Set(value string) error {
set := func(k, v string) {
var val *string
if v != "" {
val = &v
}
*labels = append(*labels, mesos.Label{
Key: k,
Value: val,
})
}
e := strings.IndexRune(value, '=')
c := strings.IndexRune(value, ':')
if e != -1 && e < c {
if e == 0 {
return errZeroLengthLabelKey
}
set(value[:e], value[e+1:])
} else if c != -1 && c < e {
if c == 0 {
return errZeroLengthLabelKey
}
set(value[:c], value[c+1:])
} else if e != -1 {
if e == 0 {
return errZeroLengthLabelKey
}
set(value[:e], value[e+1:])
} else if c != -1 {
if c == 0 {
return errZeroLengthLabelKey
}
set(value[:c], value[c+1:])
} else if value != "" {
set(value, "")
}
return nil
}
func (labels Labels) String() string {
// super inefficient, but it's only for occassional debugging
s := ""
valueString := func(v *string) string {
if v == nil {
return ""
}
return ":" + *v
}
for _, x := range labels {
if s != "" {
s += ","
}
s += x.Key + valueString(x.Value)
}
return s
}
|