changeset 854:83c271cb2344

Reverted fcb38cedc680 (More tightened color model.) The get request did no longer work: HTTP/1.1 500 Internal Server Error Content-Type: text/plain; charset=utf-8 X-Content-Type-Options: nosniff Date: Fri, 28 Sep 2018 11:47:14 GMT Content-Length: 65 error: sql: Scan error on column index 3: Unexpected type string
author Sascha Wilde <wilde@intevation.de>
date Fri, 28 Sep 2018 13:54:57 +0200
parents a3b2626cef49
children d464b777d98f
files pkg/controllers/system.go pkg/models/system.go
diffstat 2 files changed, 15 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/system.go	Fri Sep 28 13:45:04 2018 +0200
+++ b/pkg/controllers/system.go	Fri Sep 28 13:54:57 2018 +0200
@@ -111,6 +111,10 @@
 	attr := mux.Vars(req)["attr"]
 
 	c := input.(*models.Colour)
+	if !c.IsValid() {
+		err = JSONError{http.StatusBadRequest, "error: invalid colours"}
+		return
+	}
 
 	var res sql.Result
 	res, err = db.ExecContext(
--- a/pkg/models/system.go	Fri Sep 28 13:45:04 2018 +0200
+++ b/pkg/models/system.go	Fri Sep 28 13:54:57 2018 +0200
@@ -1,49 +1,21 @@
 package models
 
-import (
-	"database/sql/driver"
-	"encoding/json"
-	"errors"
-	"fmt"
-)
-
 type (
-	AlphaValue float64
-
 	Colour struct {
-		R byte       `json:"r"`
-		G byte       `json:"g"`
-		B byte       `json:"b"`
-		A AlphaValue `json:"a"`
+		R int     `json:"r"`
+		G int     `json:"g"`
+		B int     `json:"b"`
+		A float32 `json:"a"`
 	}
 )
 
-var errInvalidAlphaValue = errors.New("Invalid alpha value.")
-
-func (a *AlphaValue) UnmarshalJSON(data []byte) error {
-	var f float64
-	if err := json.Unmarshal(data, &f); err != nil {
-		return err
-	}
-	if f < 0 || f > 1 {
-		return errInvalidAlphaValue
-	}
-	*a = AlphaValue(f)
-	return nil
+func isByteRange(i int) bool {
+	return i >= 0 && i < 256
 }
 
-func (a AlphaValue) Value() (driver.Value, error) {
-	return float64(a), nil
+func (c Colour) IsValid() bool {
+	return isByteRange(c.R) &&
+		isByteRange(c.G) &&
+		isByteRange(c.B) &&
+		c.A >= 0 && c.A <= 1
 }
-
-func (a *AlphaValue) Scan(src interface{}) error {
-	switch v := src.(type) {
-	case int64:
-		*a = AlphaValue(v)
-	case float64:
-		*a = AlphaValue(v)
-	default:
-		return fmt.Errorf("Unexpected type %T", src)
-	}
-	return nil
-}