-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from linkdata/ui-primitive-helpers
UI primitive helpers
- Loading branch information
Showing
9 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package jaws | ||
|
||
type RLocker interface { | ||
RLock() | ||
RUnlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package jaws | ||
|
||
import "sync" | ||
|
||
var _ BoolSetter = UiBool{} | ||
|
||
// UiBool implements BoolSetter given a sync.Locker (or RLocker) and a bool pointer. | ||
type UiBool struct { | ||
L sync.Locker | ||
P *bool | ||
} | ||
|
||
func (ui UiBool) JawsGetBool(e *Element) (val bool) { | ||
if rl, ok := ui.L.(RLocker); ok { | ||
rl.RLock() | ||
val = *ui.P | ||
rl.RUnlock() | ||
return | ||
} | ||
ui.L.Lock() | ||
val = *ui.P | ||
ui.L.Unlock() | ||
return | ||
} | ||
|
||
func (ui UiBool) JawsSetBool(e *Element, val bool) (err error) { | ||
ui.L.Lock() | ||
*ui.P = val | ||
ui.L.Unlock() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jaws | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestUiBool(t *testing.T) { | ||
var l sync.Mutex | ||
var rl sync.RWMutex | ||
var val bool | ||
|
||
ui := UiBool{L: &l, P: &val} | ||
|
||
if ui.JawsGetBool(nil) { | ||
t.Fail() | ||
} | ||
|
||
if x := ui.JawsSetBool(nil, true); x != nil { | ||
t.Error(x) | ||
} | ||
|
||
ui.L = &rl | ||
|
||
if !ui.JawsGetBool(nil) { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package jaws | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
var _ FloatSetter = UiFloat{} | ||
|
||
// UiFloat implements FloatSetter given a sync.Locker (or RLocker) and a float64 pointer. | ||
type UiFloat struct { | ||
L sync.Locker | ||
P *float64 | ||
} | ||
|
||
func (ui UiFloat) JawsGetFloat(e *Element) (val float64) { | ||
if rl, ok := ui.L.(RLocker); ok { | ||
rl.RLock() | ||
val = *ui.P | ||
rl.RUnlock() | ||
return | ||
} | ||
ui.L.Lock() | ||
val = *ui.P | ||
ui.L.Unlock() | ||
return | ||
} | ||
|
||
func (ui UiFloat) JawsSetFloat(e *Element, val float64) (err error) { | ||
ui.L.Lock() | ||
*ui.P = val | ||
ui.L.Unlock() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jaws | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestUiFloat(t *testing.T) { | ||
var l sync.Mutex | ||
var rl sync.RWMutex | ||
var val float64 | ||
|
||
ui := UiFloat{L: &l, P: &val} | ||
|
||
if ui.JawsGetFloat(nil) != 0 { | ||
t.Fail() | ||
} | ||
|
||
if x := ui.JawsSetFloat(nil, -1); x != nil { | ||
t.Error(x) | ||
} | ||
|
||
ui.L = &rl | ||
|
||
if ui.JawsGetFloat(nil) != -1 { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package jaws | ||
|
||
import ( | ||
"html" | ||
"html/template" | ||
"sync" | ||
) | ||
|
||
var _ StringSetter = UiString{} | ||
|
||
// UiString implements StringSetter and HtmlGetter given a sync.Locker (or RLocker) and a string pointer. | ||
type UiString struct { | ||
L sync.Locker | ||
P *string | ||
} | ||
|
||
func (ui UiString) JawsGetString(e *Element) (val string) { | ||
if rl, ok := ui.L.(RLocker); ok { | ||
rl.RLock() | ||
val = *ui.P | ||
rl.RUnlock() | ||
return | ||
} | ||
ui.L.Lock() | ||
val = *ui.P | ||
ui.L.Unlock() | ||
return | ||
} | ||
|
||
func (ui UiString) JawsSetString(e *Element, val string) (err error) { | ||
ui.L.Lock() | ||
*ui.P = val | ||
ui.L.Unlock() | ||
return | ||
} | ||
|
||
func (ui UiString) JawsGetHtml(e *Element) (val template.HTML) { | ||
val = template.HTML(html.EscapeString(ui.JawsGetString(e))) // #nosec G203 | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jaws | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestUiString(t *testing.T) { | ||
var l sync.Mutex | ||
var rl sync.RWMutex | ||
var val string | ||
|
||
ui := UiString{L: &l, P: &val} | ||
|
||
if ui.JawsGetString(nil) != "" { | ||
t.Fail() | ||
} | ||
|
||
if x := ui.JawsSetString(nil, "foo<"); x != nil { | ||
t.Error(x) | ||
} | ||
|
||
ui.L = &rl | ||
|
||
if ui.JawsGetHtml(nil) != "foo<" { | ||
t.Fail() | ||
} | ||
} |