Merged in validate-before-set (pull request #378)

revid/config/parameter: validate set val passed and then only if good we set receiver

Approved-by: kortschak <dan@kortschak.io>
This commit is contained in:
Saxon Milton 2020-02-16 22:25:43 +00:00
parent 81aa35b586
commit e312bca992
2 changed files with 99 additions and 64 deletions

View File

@ -209,40 +209,55 @@ func ({{.R}} *{{.N}}) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w",err)
}
{{if ne .Min .Max}}
if _v < {{.Min}} || _v > {{.Max}} {
return fmt.Errorf("invalid value %v",_v)
}
{{end}}
*{{.R}} = {{.N}}(_v)
{{else if eq .BT "uint"}}
_v, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("could not convert set string to int: %w",err)
}
{{if ne .Min .Max}}
if _v < {{.Min}} || _v > {{.Max}} {
return fmt.Errorf("invalid value %v",_v)
}
{{end}}
*{{.R}} = {{.N}}(_v)
{{else if eq .BT "float64"}}
_v, err := strconv.ParseFloat(val,64)
if err != nil {
return fmt.Errorf("could not convert set string to float: %w",err)
}
*{{.R}} = {{.N}}(_v)
{{if ne .Min .Max}}
if _v < {{.Min}} || _v > {{.Max}} {
return fmt.Errorf("invalid value %v",_v)
}
{{end}}
*{{.R}} = {{.N}}(_v)
{{else if eq .BT "time.Duration"}}
_v, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("could not convert set string to int: %w",err)
}
*{{.R}} = {{.N}}(time.Duration(_v)*time.Second)
{{else if .M}}
vals := strings.Split(val, ",")
*{{.R}} = make({{.BT}}, len(vals))
{{else if .M}}
vals := strings.Split(val, ",")
*{{.R}} = make({{.BT}}, len(vals))
for i, v := range vals {
switch v {
{{- $receiver := .R}}
{{- $m := .M}}
{{range .E}}case "{{ . }}":
(*{{$receiver}})[i] = {{$m}}{{ . }}
{{end -}}
default:
return fmt.Errorf("unrecognised {{.N}}: %s",val)
}
}
for i, v := range vals {
switch v {
{{- $receiver := .R}}
{{- $m := .M}}
{{range .E}}case "{{ . }}":
(*{{$receiver}})[i] = {{$m}}{{ . }}
{{end -}}
default:
return fmt.Errorf("unrecognised {{.N}}: %s",val)
}
}
{{else}}
switch val {
{{- $receiver := .R}}
@ -255,12 +270,6 @@ func ({{.R}} *{{.N}}) Set(val string) error {
}
{{end -}}
{{- if and (or (or (eq .BT "int") (eq .BT "uint")) (eq .BT "float64")) (ne .Min .Max)}}
if *{{.R}} < {{.Min}} || *{{.R}} > {{.Max}} {
return fmt.Errorf("invalid value %v",*{{.R}})
}
{{end -}}
return nil
}
`

View File

@ -1,6 +1,6 @@
/*
DESCRIPTION
Code generated by "go build generate_parameters.go; ./generate_parameters; DO NOT EDIT.
Code generated by "go run generate_parameters.go; DO NOT EDIT.
parameters.go contains implementations of the Parameter interface for all
parameter types required by the configuration struct.
@ -93,6 +93,7 @@ func (b *BitDepth) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*b = BitDepth(_v)
return nil
}
@ -105,11 +106,12 @@ func (b *Bitrate) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*b = Bitrate(_v)
if *b < 1000 || *b > 10000000 {
return fmt.Errorf("invalid value %v", *b)
if _v < 1000 || _v > 10000000 {
return fmt.Errorf("invalid value %v", _v)
}
*b = Bitrate(_v)
return nil
}
@ -121,11 +123,12 @@ func (b *Brightness) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*b = Brightness(_v)
if *b < 0 || *b > 100 {
return fmt.Errorf("invalid value %v", *b)
if _v < 0 || _v > 100 {
return fmt.Errorf("invalid value %v", _v)
}
*b = Brightness(_v)
return nil
}
@ -192,6 +195,7 @@ func (c *Channels) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*c = Channels(_v)
return nil
}
@ -297,11 +301,12 @@ func (f *FileFPS) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*f = FileFPS(_v)
if *f < 1 || *f > 30 {
return fmt.Errorf("invalid value %v", *f)
if _v < 1 || _v > 30 {
return fmt.Errorf("invalid value %v", _v)
}
*f = FileFPS(_v)
return nil
}
@ -345,11 +350,12 @@ func (f *FrameRate) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*f = FrameRate(_v)
if *f < 1 || *f > 30 {
return fmt.Errorf("invalid value %v", *f)
if _v < 1 || _v > 30 {
return fmt.Errorf("invalid value %v", _v)
}
*f = FrameRate(_v)
return nil
}
@ -369,11 +375,12 @@ func (h *Height) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*h = Height(_v)
if *h < 360 || *h > 1080 {
return fmt.Errorf("invalid value %v", *h)
if _v < 360 || _v > 1080 {
return fmt.Errorf("invalid value %v", _v)
}
*h = Height(_v)
return nil
}
@ -463,11 +470,12 @@ func (m *MinFPS) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MinFPS(_v)
if *m < 1 || *m > 30 {
return fmt.Errorf("invalid value %v", *m)
if _v < 1 || _v > 30 {
return fmt.Errorf("invalid value %v", _v)
}
*m = MinFPS(_v)
return nil
}
@ -479,11 +487,12 @@ func (m *MinFrames) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MinFrames(_v)
if *m < 0 || *m > 1000 {
return fmt.Errorf("invalid value %v", *m)
if _v < 0 || _v > 1000 {
return fmt.Errorf("invalid value %v", _v)
}
*m = MinFrames(_v)
return nil
}
@ -521,6 +530,7 @@ func (m *MotionDownscaling) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MotionDownscaling(_v)
return nil
}
@ -533,6 +543,7 @@ func (m *MotionHistory) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MotionHistory(_v)
return nil
}
@ -545,11 +556,12 @@ func (m *MotionInterval) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MotionInterval(_v)
if *m < 0 || *m > 30 {
return fmt.Errorf("invalid value %v", *m)
if _v < 0 || _v > 30 {
return fmt.Errorf("invalid value %v", _v)
}
*m = MotionInterval(_v)
return nil
}
@ -561,6 +573,7 @@ func (m *MotionKernel) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MotionKernel(_v)
return nil
}
@ -573,6 +586,7 @@ func (m *MotionMinArea) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to float: %w", err)
}
*m = MotionMinArea(_v)
return nil
}
@ -585,6 +599,7 @@ func (m *MotionPixels) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*m = MotionPixels(_v)
return nil
}
@ -597,6 +612,7 @@ func (m *MotionThreshold) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to float: %w", err)
}
*m = MotionThreshold(_v)
return nil
}
@ -679,6 +695,7 @@ func (q *Quantization) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*q = Quantization(_v)
return nil
}
@ -691,11 +708,12 @@ func (r *RBCapacity) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*r = RBCapacity(_v)
if *r < 1000000 || *r > 100000000 {
return fmt.Errorf("invalid value %v", *r)
if _v < 1000000 || _v > 100000000 {
return fmt.Errorf("invalid value %v", _v)
}
*r = RBCapacity(_v)
return nil
}
@ -707,11 +725,12 @@ func (r *RBMaxElements) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*r = RBMaxElements(_v)
if *r < 0 || *r > 4294967295 {
return fmt.Errorf("invalid value %v", *r)
if _v < 0 || _v > 4294967295 {
return fmt.Errorf("invalid value %v", _v)
}
*r = RBMaxElements(_v)
return nil
}
@ -751,6 +770,7 @@ func (r *RecPeriod) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to float: %w", err)
}
*r = RecPeriod(_v)
return nil
}
@ -763,11 +783,12 @@ func (r *Rotation) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*r = Rotation(_v)
if *r < 0 || *r > 359 {
return fmt.Errorf("invalid value %v", *r)
if _v < 0 || _v > 359 {
return fmt.Errorf("invalid value %v", _v)
}
*r = Rotation(_v)
return nil
}
@ -779,6 +800,7 @@ func (s *SampleRate) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*s = SampleRate(_v)
return nil
}
@ -791,11 +813,12 @@ func (s *Saturation) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*s = Saturation(_v)
if *s < -50 || *s > 50 {
return fmt.Errorf("invalid value %v", *s)
if _v < -50 || _v > 50 {
return fmt.Errorf("invalid value %v", _v)
}
*s = Saturation(_v)
return nil
}
@ -822,11 +845,12 @@ func (v *VBRBitrate) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*v = VBRBitrate(_v)
if *v < 1 || *v > 30 {
return fmt.Errorf("invalid value %v", *v)
if _v < 1 || _v > 30 {
return fmt.Errorf("invalid value %v", _v)
}
*v = VBRBitrate(_v)
return nil
}
@ -882,11 +906,12 @@ func (w *Width) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to int: %w", err)
}
*w = Width(_v)
if *w < 640 || *w > 1920 {
return fmt.Errorf("invalid value %v", *w)
if _v < 640 || _v > 1920 {
return fmt.Errorf("invalid value %v", _v)
}
*w = Width(_v)
return nil
}
@ -898,6 +923,7 @@ func (w *WriteRate) Set(val string) error {
if err != nil {
return fmt.Errorf("could not convert set string to float: %w", err)
}
*w = WriteRate(_v)
return nil
}