This commit is contained in:
a 2023-06-11 09:19:01 -05:00
parent 5e9454d225
commit 9d961ec0ba
6 changed files with 27 additions and 23 deletions

View File

@ -65,7 +65,7 @@ func _main() error {
handler.AddDomain[domains.Todo](h, "monitor")
handler.AddDomain[domains.Wm](h, "wm")
handler.AddDomain[domains.Todo](h, "rule")
handler.AddDomain[domains.Todo](h, "config")
handler.AddDomain[domains.Config](h, "config")
handler.AddDomain[domains.Todo](h, "subscribe")
handler.AddDomain[domains.Todo](h, "quit")
handler.AddDomain[domains.Query](h, "query")

View File

@ -73,9 +73,6 @@ func (m *Modifier[T]) FillDefaults() {
ft := rt.Field(i)
k := ft.Tag.Get("cfg")
dv := ft.Tag.Get("default")
if dv == "" {
continue
}
m.Set(k, dv)
}
return
@ -92,11 +89,14 @@ func (m *Modifier[T]) setup() {
kind := ft.Type.Elem().Kind()
switch kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
m.setters[k] = func(v string) error {
val, err := strconv.ParseUint(v, 10, 64)
m.setters[k] = func(v string) (err error) {
var val uint64
if v != "" {
val, err = strconv.ParseUint(v, 10, 64)
if err != nil {
return copies.NewInvalidValueErr(k, v)
}
}
if fv.IsNil() {
fv.Set(reflect.New(ft.Type.Elem()))
}
@ -110,11 +110,14 @@ func (m *Modifier[T]) setup() {
return fmt.Sprintf("%v", fv.Elem()), nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
m.setters[k] = func(v string) error {
val, err := strconv.Atoi(v)
m.setters[k] = func(v string) (err error) {
var val int
if v != "" {
val, err = strconv.Atoi(v)
if err != nil {
return copies.NewInvalidValueErr(k, v)
}
}
if fv.IsNil() {
fv.Set(reflect.New(ft.Type.Elem()))
}
@ -133,7 +136,7 @@ func (m *Modifier[T]) setup() {
switch strings.ToLower(v) {
case "true", "on":
b = true
case "false", "off":
case "false", "off", "":
b = false
default:
return copies.NewInvalidValueErr(k, v)

View File

@ -24,8 +24,6 @@ type s1 struct {
func TestModifierS1(t *testing.T) {
s := &s1{}
m := cfg.NewModifier(s)
m.FillDefaults()
assert.EqualValues(t, *m.Ref.C, "crabs")
m.Set("b", "on")
assert.EqualValues(t, *m.Ref.B, true)
@ -33,4 +31,7 @@ func TestModifierS1(t *testing.T) {
m.Set("d", "442")
assert.EqualValues(t, *m.Ref.D, 442)
m.FillDefaults()
assert.EqualValues(t, *m.Ref.C, "crabs")
}

View File

@ -1,8 +1,6 @@
package domains
import (
"fmt"
"tuxpa.in/t/wm/src/copies"
"tuxpa.in/t/wm/src/sock"
)
@ -61,6 +59,6 @@ func (n *Config) parse(msg *sock.Msg) (bool, error) {
if msg.HasNext() {
return false, &copies.ErrInvalidArgumentCount{Name: "2 or 3", Value: len(msg.Args())}
}
return false, fmt.Errorf(`unknown option: '%s'`, arg)
return false, nil
}
}

View File

@ -33,11 +33,11 @@ func (h *Handler) Run(msg *sock.Msg) {
if msg.Err(err) {
return
}
err = msg.Reply(string(resp))
err = msg.Reply(resp)
if msg.Err(err) {
return
}
msg.Reply("")
msg.Reply(nil)
}
func (h *Handler) run(msg *sock.Msg) ([]byte, error) {

View File

@ -56,17 +56,19 @@ func (m *Msg) Err(e error) bool {
wr.Flush()
return true
}
func (m *Msg) Reply(ans string) error {
func (m *Msg) Reply(xs []byte) error {
if !m.closed.CompareAndSwap(false, true) {
return nil
}
defer m.c.Close()
wr := bufio.NewWriter(m.c)
_, err := wr.Write([]byte(ans))
_, err := wr.Write(xs)
if err != nil {
return err
}
if len(xs) != 0 {
wr.Write([]byte("\n"))
}
wr.Write([]byte{0})
wr.Flush()
return nil