diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 04e8bb3..56a30a0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,5 +15,13 @@ jobs: with: go-version: 1.15 + - name: Install dependencies + run: | + go version + go get -u golang.org/x/lint/golint + + - name: Lint + run: golint ./... + - name: Test run: go test -v ./... diff --git a/context.go b/context.go index b135f4a..0cb9f4c 100644 --- a/context.go +++ b/context.go @@ -2,5 +2,8 @@ package inertia type contextKey string +// ContextKeyProps key. const ContextKeyProps = contextKey("props") + +// ContextKeyViewData key. const ContextKeyViewData = contextKey("viewData") diff --git a/errors.go b/errors.go index 25ea1c9..29c311b 100644 --- a/errors.go +++ b/errors.go @@ -3,6 +3,9 @@ package inertia import "errors" var ( - ErrInvalidContextProps = errors.New("inertia: could not convert context props to map") + // ErrInvalidContextProps error. + ErrInvalidContextProps = errors.New("inertia: could not convert context props to map") + + // ErrInvalidContextViewData error. ErrInvalidContextViewData = errors.New("inertia: could not convert context view data to map") ) diff --git a/inertia.go b/inertia.go index 88c7455..1844653 100644 --- a/inertia.go +++ b/inertia.go @@ -9,6 +9,7 @@ import ( "strings" ) +// Inertia type. type Inertia struct { url string rootTemplate string @@ -17,6 +18,7 @@ type Inertia struct { sharedFuncMap template.FuncMap } +// New function. func New(url, rootTemplate, version string) *Inertia { i := new(Inertia) i.url = url @@ -28,14 +30,17 @@ func New(url, rootTemplate, version string) *Inertia { return i } +// Share function. func (i *Inertia) Share(key string, value interface{}) { i.sharedProps[key] = value } +// ShareFunc function. func (i *Inertia) ShareFunc(key string, value interface{}) { i.sharedFuncMap[key] = value } +// WithProp function. func (i *Inertia) WithProp(ctx context.Context, key string, value interface{}) context.Context { contextProps := ctx.Value(ContextKeyProps) @@ -53,6 +58,7 @@ func (i *Inertia) WithProp(ctx context.Context, key string, value interface{}) c }) } +// WithViewData function. func (i *Inertia) WithViewData(ctx context.Context, key string, value interface{}) context.Context { contextViewData := ctx.Value(ContextKeyViewData) @@ -70,6 +76,7 @@ func (i *Inertia) WithViewData(ctx context.Context, key string, value interface{ }) } +// Render function. func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component string, props map[string]interface{}) error { only := make(map[string]string) partial := r.Header.Get("X-Inertia-Partial-Data") @@ -83,7 +90,7 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin page := &Page{ Component: component, Props: make(map[string]interface{}), - Url: r.RequestURI, + URL: r.RequestURI, Version: i.version, } diff --git a/middleware.go b/middleware.go index 857326a..1824463 100644 --- a/middleware.go +++ b/middleware.go @@ -2,6 +2,7 @@ package inertia import "net/http" +// Middleware function. func (i *Inertia) Middleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("X-Inertia") == "" { diff --git a/page.go b/page.go index 58af516..4179904 100644 --- a/page.go +++ b/page.go @@ -1,8 +1,9 @@ package inertia +// Page type. type Page struct { Component string `json:"component"` Props map[string]interface{} `json:"props"` - Url string `json:"url"` + URL string `json:"url"` Version string `json:"version"` }