basic HTTP api and uistate.

This commit is contained in:
2023-01-01 16:30:40 +01:00
parent 865dd1994a
commit fe5bfc1efc
14 changed files with 583 additions and 21 deletions
+16
View File
@@ -1,6 +1,7 @@
package color
import (
"encoding/json"
"fmt"
"github.com/lucasb-eyer/go-colorful"
"math"
@@ -15,6 +16,21 @@ type Color struct {
XY *XY `json:"xy,omitempty"`
}
func (col *Color) MarshalJSON() ([]byte, error) {
return json.Marshal(col.String())
}
func (col *Color) UnmarshalJSON(bytes []byte) error {
var s string
err := json.Unmarshal(bytes, &s)
if err != nil {
return err
}
*col, err = Parse(s)
return err
}
func (col *Color) IsHueSat() bool {
return col.HS != nil
}
+66
View File
@@ -0,0 +1,66 @@
package gentools
func ApplyUpdate[T any](dst *T, value *T) {
if value != nil {
*dst = *value
}
}
func ApplyUpdatePtr[T any](dst **T, value *T) {
if value != nil {
*dst = ShallowCopy(value)
}
}
func ApplyUpdateSlice[T any](dst *[]T, value []T) {
if value != nil {
*dst = append([]T{}, value...)
}
}
func ApplyUpdateNonZero[T comparable](dst *T, value *T) {
var zero T
if value != nil && *value != zero {
*dst = *value
}
}
func ApplyUpdateNilZero[T comparable](dst **T, value *T) {
if value != nil {
var zero T
if *value == zero {
*dst = nil
} else {
valueCopy := *value
*dst = &valueCopy
}
}
}
func ApplyMapUpdate[K comparable, V any](dst *map[K]V, src map[K]*V) {
if *dst == nil {
dst = &map[K]V{}
}
for key, value := range src {
if value != nil {
(*dst)[key] = *value
} else {
delete(*dst, key)
}
}
}
func ApplyUpdateMapNilZero[K comparable, V comparable](dst *map[K]V, src map[K]V) {
var zero V
if *dst == nil {
dst = &map[K]V{}
}
for key, value := range src {
if value != zero {
(*dst)[key] = value
} else {
delete(*dst, key)
}
}
}