Skip to content

Commit

Permalink
Merge pull request #51 from linkdata/getset-refactor
Browse files Browse the repository at this point in the history
Getset refactor
  • Loading branch information
linkdata authored Dec 13, 2024
2 parents 13a1ffe + ee6be70 commit 7af84d4
Show file tree
Hide file tree
Showing 75 changed files with 577 additions and 1,953 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ router.GET("/jaws/*", func(c echo.Context) error {
})
```

### HTML rendering

HTML output elements (e.g. `jaws.RequestWriter.Div()`) require a `jaws.HtmlGetter` or something that can
be made into one using `jaws.MakeHtmlGetter()`.

In order of precedence, this can be:
* `jaws.HtmlGetter`: `JawsGetHtml(*Element) template.HTML` to be used as-is.
* `jaws.Getter[template.HTML]`: `JawsGet(*Element) template.HTML` to be used as-is.
* `jaws.StringGetter`: `JawsGetString(*Element) string` that will be escaped using `html.EscapeString`.
* `jaws.Getter[string]`: `JawsGet(*Element) string` that will be escaped using `html.EscapeString`.
* `jaws.AnyGetter`: `JawsGetAny(*Element) any` that will be rendered using `fmt.Sprint()` and escaped using `html.EscapeString`.
* `fmt.Stringer`: `String() string` that will be escaped using `html.EscapeString`.
* a static `template.HTML` or `string` to be used as-is with no HTML escaping.
* everything else is rendered using `fmt.Sprint()` and escaped using `html.EscapeString`.

### Data binding

HTML input elements (e.g. `jaws.RequestWriter.Range()`) require bi-directional data flow between the server and the browser.
The first argument to these is usually a `Setter[T]` where `T` is one of `string`, `float64`, `bool` or `time.Time`. It can
also be a `Getter[T]`, in which case the HTML element should be made read-only.

You can also use a `Binder[T]` that combines a (RW)Locker and a pointer to the value, and allows you to add chained setters,
getters and on-success handlers. It can be used as a `jaws.HtmlGetter`.

### Session handling

JaWS has non-persistent session handling integrated. Sessions won't
Expand Down
23 changes: 23 additions & 0 deletions anygetter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jaws

import "errors"

var ErrValueNotSettable = errors.New("value not settable")

type AnyGetter interface {
JawsGetAny(elem *Element) (value any)
}

type anyGetter struct{ v any }

func (g anyGetter) JawsGetAny(e *Element) any {
return g.v
}

func (g anyGetter) JawsSetAny(*Element, any) error {
return ErrValueNotSettable
}

func (g anyGetter) JawsGetTag(rq *Request) any {
return nil
}
23 changes: 2 additions & 21 deletions anysetter.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
package jaws

import (
"sync/atomic"
)

type AnySetter interface {
JawsGetAny(e *Element) any
AnyGetter
// JawsSetAny may return ErrValueUnchanged to indicate value was already set.
// It may panic if the type of v cannot be handled.
JawsSetAny(e *Element, v any) (err error)
}

type anyGetter struct{ v any }

func (g anyGetter) JawsGetAny(e *Element) any {
return g.v
}

func (g anyGetter) JawsSetAny(*Element, any) error {
return ErrValueNotSettable
}

func (g anyGetter) JawsGetTag(rq *Request) any {
return nil
}

func makeAnySetter(v any) AnySetter {
switch v := v.(type) {
case AnySetter:
return v
case *atomic.Value:
return atomicSetter{v}
}
return anyGetter{v}
}
8 changes: 0 additions & 8 deletions anysetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ func Test_makeAnySetter(t *testing.T) {
err: ErrValueNotSettable,
tag: nil,
},
{
name: "*atomic.Value",
v: &av,
want: atomicSetter{&av},
in: -val,
out: val,
tag: &av,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
94 changes: 0 additions & 94 deletions atomicsetter.go

This file was deleted.

156 changes: 0 additions & 156 deletions atomicsetter_test.go

This file was deleted.

Loading

0 comments on commit 7af84d4

Please sign in to comment.