IntOrString for use in JSON/YAML

Specifying an API type as IntOrString will allow JSON and YAML to accept
either ints or strings with the same name.  For example, port names or
numbers.
Arguably a panic is better than an error - more obvious.  It's as close to
a compile error as it can get.  Still, if you guys say returning an error
is better, so be it.

:)


On Fri, Jul 11, 2014 at 10:00 AM, roberthbailey <notifications@github.com>
wrote:

> In pkg/util/util.go:
>
> > +	if value[0] == '"' {
> > +		intstr.Kind = IntstrString
> > +		return json.Unmarshal(value, &intstr.StrVal)
> > +	}
> > +	intstr.Kind = IntstrInt
> > +	return json.Unmarshal(value, &intstr.IntVal)
> > +}
> > +
> > +func (intstr IntOrString) MarshalJSON() ([]byte, error) {
> > +	switch intstr.Kind {
> > +	case IntstrInt:
> > +		return json.Marshal(intstr.IntVal)
> > +	case IntstrString:
> > +		return json.Marshal(intstr.StrVal)
> > +	default:
> > +		panic("impossible IntOrString.Kind")
>
> Yeah, I wasn't sure what to recommend there. It's impossible now, but if
> you ever extended IntstrKind to have a third kind and forgot to update the
> yaml conversion code, we wouldn't notice during compilation and we'd start
> seeing programs crashing with panics.
>
> --
> Reply to this email directly or view it on GitHub
> <https://github.com/GoogleCloudPlatform/kubernetes/pull/395/files#r14831291>
> .
>