| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 10 |
"github.com/mesos/mesos-go/api/v1/lib/encoding" |
| 11 |
"github.com/mesos/mesos-go/api/v1/lib/encoding/codecs" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
errZeroLengthLabelKey = errors.New("zero-length label key") |
| 16 |
) |
| 17 |
|
| 18 |
type URL struct{ url.URL } |
| 19 |
|
| 20 |
func (u *URL) Set(value string) error { |
| 21 |
parsed, err := url.Parse(value) |
| 22 |
if err != nil { |
| 23 |
return err |
| 24 |
} |
| 25 |
u.URL = *parsed |
| 26 |
return nil |
| 27 |
} |
| 28 |
|
| 29 |
type codec struct{ encoding.Codec } |
| 30 |
|
| 31 |
func (c *codec) Set(value string) error { |
| 32 |
v := strings.ToLower(value) |
| 33 |
for _, codec := range codecs.ByMediaType { |
| 34 |
if v == codec.Name { |
| 35 |
c.Codec = codec |
| 36 |
return nil |
| 37 |
} |
| 38 |
} |
| 39 |
return fmt.Errorf("bad codec %q", value) |
| 40 |
} |
| 41 |
|
| 42 |
type Labels []mesos.Label |
| 43 |
|
| 44 |
func (labels *Labels) Set(value string) error { |
| 45 |
set := func(k, v string) { |
| 46 |
var val *string |
| 47 |
if v != "" { |
| 48 |
val = &v |
| 49 |
} |
| 50 |
*labels = append(*labels, mesos.Label{ |
| 51 |
Key: k, |
| 52 |
Value: val, |
| 53 |
}) |
| 54 |
} |
| 55 |
e := strings.IndexRune(value, '=') |
| 56 |
c := strings.IndexRune(value, ':') |
| 57 |
if e != -1 && e < c { |
| 58 |
if e == 0 { |
| 59 |
return errZeroLengthLabelKey |
| 60 |
} |
| 61 |
set(value[:e], value[e+1:]) |
| 62 |
} else if c != -1 && c < e { |
| 63 |
if c == 0 { |
| 64 |
return errZeroLengthLabelKey |
| 65 |
} |
| 66 |
set(value[:c], value[c+1:]) |
| 67 |
} else if e != -1 { |
| 68 |
if e == 0 { |
| 69 |
return errZeroLengthLabelKey |
| 70 |
} |
| 71 |
set(value[:e], value[e+1:]) |
| 72 |
} else if c != -1 { |
| 73 |
if c == 0 { |
| 74 |
return errZeroLengthLabelKey |
| 75 |
} |
| 76 |
set(value[:c], value[c+1:]) |
| 77 |
} else if value != "" { |
| 78 |
set(value, "") |
| 79 |
} |
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
func (labels Labels) String() string { |
| 84 |
|
| 85 |
s := "" |
| 86 |
valueString := func(v *string) string { |
| 87 |
if v == nil { |
| 88 |
return "" |
| 89 |
} |
| 90 |
return ":" + *v |
| 91 |
} |
| 92 |
for _, x := range labels { |
| 93 |
if s != "" { |
| 94 |
s += "," |
| 95 |
} |
| 96 |
s += x.Key + valueString(x.Value) |
| 97 |
} |
| 98 |
return s |
| 99 |
} |