Skip to content

Commit

Permalink
Merge pull request #30 from linkdata/pass-jid-to-all-eventfn
Browse files Browse the repository at this point in the history
add jid parameter to event functions
  • Loading branch information
linkdata authored Jul 20, 2023
2 parents 0c11bc7 + 6566929 commit 136b966
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
10 changes: 5 additions & 5 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/linkdata/jaws/what"
)

type ClickFn func(rq *Request) error
type InputTextFn func(rq *Request, val string) error
type InputFloatFn func(rq *Request, val float64) error
type InputBoolFn func(rq *Request, val bool) error
type InputDateFn func(rq *Request, val time.Time) error
type ClickFn func(rq *Request, jid string) error
type InputTextFn func(rq *Request, jid string, val string) error
type InputFloatFn func(rq *Request, jid string, val float64) error
type InputBoolFn func(rq *Request, jid string, val bool) error
type InputDateFn func(rq *Request, jid string, val time.Time) error

const ISO8601 = "2006-01-02"

Expand Down
2 changes: 1 addition & 1 deletion namedboolarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (nba *NamedBoolArray) radioEventFn(rq *Request, evt what.What, jid, val str
nba.mu.Unlock()
rq.SetBoolValue(jid, v)
if fn != nil {
err = fn(rq, name)
err = fn(rq, jid, name)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion radiogrouper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jaws

type RadioGrouper interface {
JawsRadioGroupData() *NamedBoolArray
JawsRadioGroupHandler(rq *Request, boolName string) error
JawsRadioGroupHandler(rq *Request, jid, boolName string) error
}

func (rq *Request) RadioGroup(grouper RadioGrouper) (rl []Radio) {
Expand Down
24 changes: 12 additions & 12 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,17 @@ func makeAlertDangerMessage(err error) (msg *Message) {
return
}

func (rq *Request) maybeEvent(event what.What, id string, fn ClickFn) string {
func (rq *Request) maybeEvent(event what.What, jid string, fn ClickFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
wf = func(rq *Request, evt what.What, jid, val string) (err error) {
if evt == event {
err = fn(rq)
err = fn(rq, jid)
}
return
}
}
return rq.RegisterEventFn(id, wf)
return rq.RegisterEventFn(jid, wf)
}

func (rq *Request) maybeClick(jid string, fn ClickFn) string {
Expand All @@ -528,9 +528,9 @@ func (rq *Request) maybeClick(jid string, fn ClickFn) string {
func (rq *Request) maybeInputText(jid string, fn InputTextFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
wf = func(rq *Request, evt what.What, jid, val string) (err error) {
if evt == what.Input {
err = fn(rq, val)
err = fn(rq, jid, val)
}
return
}
Expand All @@ -541,15 +541,15 @@ func (rq *Request) maybeInputText(jid string, fn InputTextFn) string {
func (rq *Request) maybeInputFloat(jid string, fn InputFloatFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
wf = func(rq *Request, evt what.What, jid, val string) (err error) {
if evt == what.Input {
var v float64
if val != "" {
if v, err = strconv.ParseFloat(val, 64); err != nil {
return
}
}
err = fn(rq, v)
err = fn(rq, jid, v)
}
return
}
Expand All @@ -560,15 +560,15 @@ func (rq *Request) maybeInputFloat(jid string, fn InputFloatFn) string {
func (rq *Request) maybeInputBool(jid string, fn InputBoolFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
wf = func(rq *Request, evt what.What, jid, val string) (err error) {
if evt == what.Input {
var v bool
if val != "" {
if v, err = strconv.ParseBool(val); err != nil {
return
}
}
err = fn(rq, v)
err = fn(rq, jid, v)
}
return
}
Expand All @@ -579,15 +579,15 @@ func (rq *Request) maybeInputBool(jid string, fn InputBoolFn) string {
func (rq *Request) maybeInputDate(jid string, fn InputDateFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
wf = func(rq *Request, evt what.What, jid, val string) (err error) {
if evt == what.Input {
var v time.Time
if val != "" {
if v, err = time.Parse(ISO8601, val); err != nil {
return
}
}
err = fn(rq, v)
err = fn(rq, jid, v)
}
return
}
Expand Down
37 changes: 24 additions & 13 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ func TestRequest_OnInput(t *testing.T) {
gotCall := make(chan struct{})
rq := newTestRequest(is)
defer rq.Close()
is.NoErr(rq.OnInput(elemId, func(rq *Request, val string) error {
is.NoErr(rq.OnInput(elemId, func(rq *Request, jid, val string) error {
defer close(gotCall)
is.Equal(jid, elemId)
is.Equal(val, elemVal)
return nil
}))
Expand All @@ -591,8 +592,9 @@ func TestRequest_OnClick(t *testing.T) {
gotCall := make(chan struct{})
rq := newTestRequest(is)
defer rq.Close()
is.NoErr(rq.OnClick(elemId, func(rq *Request) error {
is.NoErr(rq.OnClick(elemId, func(rq *Request, jid string) error {
defer close(gotCall)
is.Equal(jid, elemId)
return nil
}))
rq.inCh <- &Message{Elem: elemId, What: what.Click, Data: elemVal}
Expand All @@ -610,8 +612,9 @@ func TestRequest_OnTrigger(t *testing.T) {
gotCall := make(chan struct{})
rq := newTestRequest(is)
defer rq.Close()
is.NoErr(rq.OnTrigger(elemId, func(rq *Request) error {
is.NoErr(rq.OnTrigger(elemId, func(rq *Request, jid string) error {
defer close(gotCall)
is.Equal(jid, elemId)
return nil
}))
rq.inCh <- &Message{Elem: elemId, What: what.Trigger, Data: elemVal}
Expand Down Expand Up @@ -668,8 +671,9 @@ func TestRequest_Text(t *testing.T) {
defer rq.Close()

gotCall := make(chan struct{})
h := rq.Text(elemId, elemVal, func(rq *Request, val string) error {
h := rq.Text(elemId, elemVal, func(rq *Request, jid, val string) error {
defer close(gotCall)
is.Equal(jid, elemId)
is.Equal(val, "other-stuff")
return nil
}, "disabled")
Expand All @@ -690,8 +694,9 @@ func TestRequest_Password(t *testing.T) {
defer rq.Close()

gotCall := make(chan struct{})
h := rq.Password(elemId, func(rq *Request, val string) error {
h := rq.Password(elemId, func(rq *Request, jid, val string) error {
defer close(gotCall)
is.Equal(jid, elemId)
is.Equal(val, "other-stuff")
return nil
}, "autocomplete=\"off\"")
Expand All @@ -714,7 +719,8 @@ func TestRequest_Number(t *testing.T) {

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Number(elemId, elemVal, func(rq *Request, val float64) error {
h := rq.Number(elemId, elemVal, func(rq *Request, jid string, val float64) error {
is.Equal(jid, elemId)
switch val {
case 4.3:
// ok
Expand Down Expand Up @@ -759,8 +765,9 @@ func TestRequest_Range(t *testing.T) {
defer rq.Close()

gotCall := make(chan struct{})
h := rq.Range(elemId, elemVal, func(rq *Request, val float64) error {
h := rq.Range(elemId, elemVal, func(rq *Request, jid string, val float64) error {
defer close(gotCall)
is.Equal(jid, elemId)
is.Equal(val, 3.15)
return nil
}, "disabled")
Expand All @@ -783,7 +790,8 @@ func TestRequest_Checkbox(t *testing.T) {

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Checkbox(elemId, elemVal, func(rq *Request, val bool) error {
h := rq.Checkbox(elemId, elemVal, func(rq *Request, jid string, val bool) error {
is.Equal(jid, elemId)
is.Equal(val, false)
gotCall <- struct{}{}
return nil
Expand Down Expand Up @@ -822,7 +830,8 @@ func TestRequest_Date(t *testing.T) {

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Date(elemId, elemVal, func(rq *Request, val time.Time) error {
h := rq.Date(elemId, elemVal, func(rq *Request, jid string, val time.Time) error {
is.Equal(jid, elemId)
if !val.IsZero() {
is.Equal(val.Year(), 1970)
is.Equal(val.Month(), time.January)
Expand Down Expand Up @@ -862,8 +871,9 @@ func TestRequest_Radio(t *testing.T) {
defer rq.Close()

gotCall := make(chan struct{})
h := rq.Radio("quux", true, func(rq *Request, val bool) error {
h := rq.Radio("quux", true, func(rq *Request, jid string, val bool) error {
defer close(gotCall)
is.Equal(jid, "quux")
is.Equal(val, false)
return nil
})
Expand All @@ -888,8 +898,8 @@ func (rg *radioGrouper) JawsRadioGroupData() *NamedBoolArray {
return rg.NamedBoolArray
}

func (rg *radioGrouper) JawsRadioGroupHandler(rq *Request, val string) error {
return rg.fn(rq, val)
func (rg *radioGrouper) JawsRadioGroupHandler(rq *Request, jid, val string) error {
return rg.fn(rq, jid, val)
}

func TestRequest_LabeledRadioGroup(t *testing.T) {
Expand All @@ -904,8 +914,9 @@ func TestRequest_LabeledRadioGroup(t *testing.T) {
nba.SetOnly("alpha")
is.Equal(nba.Get(), "alpha")

rg := &radioGrouper{NamedBoolArray: nba, fn: func(rq *Request, val string) error {
rg := &radioGrouper{NamedBoolArray: nba, fn: func(rq *Request, jid, val string) error {
defer close(gotCall)
is.Equal(jid, "quux/bravo")
is.Equal(val, "bravo")
return nil
}}
Expand Down
47 changes: 47 additions & 0 deletions what/what_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package what

import (
"fmt"
"testing"
)

func TestParse(t *testing.T) {
lastWhat := What(len(_What_index) - 2)
tests := []struct {
name string
arg string
want What
}{
{"blank is None", "", None},
{"Inner", "Inner", Inner},
{"inner", "inner", Inner},
{"innerr", "innerr", None},
{"last", lastWhat.String(), lastWhat},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Parse(tt.arg); got != tt.want {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}
})
}
}

func TestString(t *testing.T) {
tests := []struct {
name string
arg What
want string
}{
{"None", None, "None"},
{"Inner", Inner, "Inner"},
{"unknown", What(len(_What_index) + 44), fmt.Sprintf("What(%d)", len(_What_index)+44)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.arg.String(); got != tt.want {
t.Errorf("%v.String() = %q, want %q", tt.arg, got, tt.want)
}
})
}
}

0 comments on commit 136b966

Please sign in to comment.