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.Todo](h, "monitor")
handler.AddDomain[domains.Wm](h, "wm") handler.AddDomain[domains.Wm](h, "wm")
handler.AddDomain[domains.Todo](h, "rule") 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, "subscribe")
handler.AddDomain[domains.Todo](h, "quit") handler.AddDomain[domains.Todo](h, "quit")
handler.AddDomain[domains.Query](h, "query") handler.AddDomain[domains.Query](h, "query")

View File

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

View File

@ -24,8 +24,6 @@ type s1 struct {
func TestModifierS1(t *testing.T) { func TestModifierS1(t *testing.T) {
s := &s1{} s := &s1{}
m := cfg.NewModifier(s) m := cfg.NewModifier(s)
m.FillDefaults()
assert.EqualValues(t, *m.Ref.C, "crabs")
m.Set("b", "on") m.Set("b", "on")
assert.EqualValues(t, *m.Ref.B, true) assert.EqualValues(t, *m.Ref.B, true)
@ -33,4 +31,7 @@ func TestModifierS1(t *testing.T) {
m.Set("d", "442") m.Set("d", "442")
assert.EqualValues(t, *m.Ref.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 package domains
import ( import (
"fmt"
"tuxpa.in/t/wm/src/copies" "tuxpa.in/t/wm/src/copies"
"tuxpa.in/t/wm/src/sock" "tuxpa.in/t/wm/src/sock"
) )
@ -61,6 +59,6 @@ func (n *Config) parse(msg *sock.Msg) (bool, error) {
if msg.HasNext() { if msg.HasNext() {
return false, &copies.ErrInvalidArgumentCount{Name: "2 or 3", Value: len(msg.Args())} 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) { if msg.Err(err) {
return return
} }
err = msg.Reply(string(resp)) err = msg.Reply(resp)
if msg.Err(err) { if msg.Err(err) {
return return
} }
msg.Reply("") msg.Reply(nil)
} }
func (h *Handler) run(msg *sock.Msg) ([]byte, error) { func (h *Handler) run(msg *sock.Msg) ([]byte, error) {

View File

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