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,18 +209,33 @@ func ({{.R}} *{{.N}}) Set(val string) error {
if err != nil { if err != nil {
return fmt.Errorf("could not convert set string to int: %w",err) 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) *{{.R}} = {{.N}}(_v)
{{else if eq .BT "uint"}} {{else if eq .BT "uint"}}
_v, err := strconv.Atoi(val) _v, err := strconv.Atoi(val)
if err != nil { if err != nil {
return fmt.Errorf("could not convert set string to int: %w",err) 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) *{{.R}} = {{.N}}(_v)
{{else if eq .BT "float64"}} {{else if eq .BT "float64"}}
_v, err := strconv.ParseFloat(val,64) _v, err := strconv.ParseFloat(val,64)
if err != nil { if err != nil {
return fmt.Errorf("could not convert set string to float: %w",err) return fmt.Errorf("could not convert set string to float: %w",err)
} }
{{if ne .Min .Max}}
if _v < {{.Min}} || _v > {{.Max}} {
return fmt.Errorf("invalid value %v",_v)
}
{{end}}
*{{.R}} = {{.N}}(_v) *{{.R}} = {{.N}}(_v)
{{else if eq .BT "time.Duration"}} {{else if eq .BT "time.Duration"}}
_v, err := strconv.Atoi(val) _v, err := strconv.Atoi(val)
@ -255,12 +270,6 @@ func ({{.R}} *{{.N}}) Set(val string) error {
} }
{{end -}} {{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 return nil
} }
` `

View File

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