cm

package
v0.0.0-...-184dd84 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

README

This sublibrary (someday perhaps available separately, but for now, not) is convenience wrapper for handling client state in server (fully in the cookie).

For now, the state is not encrypted at all (although we could), as I do not really see the point. The idea is not to store secrets using this, just e.g. state in various views so that the client resumes same state implicitly when they go back to a view.

Short form

Define state as normal struct, e.g.

type State struct {
  Number int `json:"n" cm:"number"`
}

and then call

err = cm.Run(r, w, &state)

Where

  • r: http.Request
  • w: http.ResponseWriter
  • state: arbitrary struct with cm + json tags (ideally empty but this will clear most of the state)

The struct is examined through reflection and cm:"field" entries are CRUDed based on "field" in the http.Request.Form. They are persisted automagically to cookie derived from the struct name, and with the json tagged names (preferably short ones, as cookie space is limited). If json definitions are not supplied, they will not be imported/exported.

Supported datatypes

Currently the code supports only following datatypes:

  • bool
  • int*
  • string
  • uint*

Other types, if tagged, cause an error.

What does it do?

The above cm.Run call will:

  • read existing json cookie with name cm.<module>.State and unmarshal JSON content from it to the State struct
  • reads query parameter 'number', and if it is set, update state instance
  • (.. same with other fields too, if any ..)
  • if any of the fields were updated, set the updated cookie for the response

TODO

  • Perhaps make the configuration also accept Path? For now, cookies are scoped to Path "/" so e.g. global state can be used to convey them; this may not be optimal always.

  • Document this better?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolFromForm

func BoolFromForm(r FormValued, key string, value *bool)

func Int64FromForm

func Int64FromForm(r FormValued, key string, value *int64) (found bool, err error)

func IntFromForm

func IntFromForm(r FormValued, key string, value *int) (found bool, err error)

func Parse

func Parse(r CookieSource, u *URLWrapper, state any) (changed bool, err error)

func Run

func Run(r *http.Request, w http.ResponseWriter, state any) error

func RunWrapper

func RunWrapper(s CookieSource, r *URLWrapper, w http.ResponseWriter, state any) error

func ToCookie

func ToCookie(state any) (*http.Cookie, error)

func ToQueryString

func ToQueryString(base string, state any) (ret string, err error)

func ToURLValues

func ToURLValues(v url.Values, state any) (err error)

func Uint64FromForm

func Uint64FromForm(r FormValued, key string, value *uint64) (found bool, err error)

func Write

func Write(w http.ResponseWriter, state any) error

Types

type CookieSource

type CookieSource interface {
	Cookie(string) (*http.Cookie, error)
}

type FormValued

type FormValued interface {
	FormValue(string) string
}

type URLWrapper

type URLWrapper url.Values

func GetWrapper

func GetWrapper(r *http.Request) (*URLWrapper, error)

func (URLWrapper) FormValue

func (self URLWrapper) FormValue(k string) string

func (URLWrapper) HasValue

func (self URLWrapper) HasValue(k string) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL