Skip to content

Commit

Permalink
Merge pull request #49 from linkdata/binds
Browse files Browse the repository at this point in the history
Binds
  • Loading branch information
linkdata authored Dec 6, 2024
2 parents b5d9d94 + b8afc85 commit a9b0a60
Show file tree
Hide file tree
Showing 7 changed files with 684 additions and 256 deletions.
49 changes: 0 additions & 49 deletions afterset.go

This file was deleted.

84 changes: 0 additions & 84 deletions afterset_test.go

This file was deleted.

93 changes: 3 additions & 90 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,11 @@ package jaws

import (
"sync"
"time"
)

// Binding combines a lock with a pointer to a value of type T, and implements Setter[T].
// It also implements BoolSetter, FloatSetter, StringSetter and TimeSetter, but will panic
// if the underlying type T is not correct.
type Binding[T comparable] struct {
L sync.Locker
P *T
}

var (
_ BoolSetter = Binding[bool]{}
_ FloatSetter = Binding[float64]{}
_ StringSetter = Binding[string]{}
_ TimeSetter = Binding[time.Time]{}
)

func (bind Binding[T]) Get() (value T) {
if rl, ok := bind.L.(RLocker); ok {
rl.RLock()
value = *bind.P
rl.RUnlock()
} else {
bind.L.Lock()
value = *bind.P
bind.L.Unlock()
}
return
}

func (bind Binding[T]) Set(value T) (err error) {
bind.L.Lock()
if value != *bind.P {
*bind.P = value
} else {
err = ErrValueUnchanged
}
bind.L.Unlock()
return
}

func (bind Binding[T]) JawsGet(*Element) T {
return bind.Get()
}

func (bind Binding[T]) JawsSet(elem *Element, value T) error {
return bind.Set(value)
}

func (bind Binding[T]) JawsGetTag(*Request) any {
return bind.P
}

func (bind Binding[T]) JawsSetString(e *Element, val string) (err error) {
return bind.JawsSet(e, any(val).(T))
}

func (bind Binding[T]) JawsGetString(e *Element) string {
return any(bind.JawsGet(e)).(string)
}

func (bind Binding[T]) JawsSetFloat(e *Element, val float64) (err error) {
return bind.JawsSet(e, any(val).(T))
}

func (bind Binding[T]) JawsGetFloat(e *Element) float64 {
return any(bind.JawsGet(e)).(float64)
}

func (bind Binding[T]) JawsSetBool(e *Element, val bool) (err error) {
return bind.JawsSet(e, any(val).(T))
}

func (bind Binding[T]) JawsGetBool(e *Element) bool {
return any(bind.JawsGet(e)).(bool)
}

func (bind Binding[T]) JawsGetTime(elem *Element) time.Time {
return any(bind.JawsGet(elem)).(time.Time)
}

func (bind Binding[T]) JawsSetTime(elem *Element, value time.Time) error {
return bind.JawsSet(elem, any(value).(T))
}

// Bind returns a Binding[T] with the given sync.Locker (or RWLocker) and a pointer to the underlying value of type T.
//
// It implements Setter[T]. It also implements BoolSetter, FloatSetter, StringSetter and TimeSetter, but will panic
// if the underlying type T is not correct.
// Bind returns a Binder[T] with the given sync.Locker (or RWLocker) and a pointer to the underlying value of type T.
//
// The pointer will be used as the UI tag.
func Bind[T comparable](l sync.Locker, p *T) Binding[T] {
return Binding[T]{L: l, P: p}
func Bind[T comparable](l sync.Locker, p *T) Binder[T] {
return Binding[T]{Locker: l, ptr: p}
}
Loading

0 comments on commit a9b0a60

Please sign in to comment.