Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 2.37 KB

File metadata and controls

53 lines (36 loc) · 2.37 KB

Type Checking

muxt check uses github.com/typelate/check.

Preconditions

An ExecuteTemplate call is checked when its template name is a literal and its data argument has a statically known type.

templates.ExecuteTemplate(w, "user-profile", data) // checked
templates.ExecuteTemplate(w, name, data)           // not checked: name is not a literal

Generated handlers meet both conditions, so every route template is checked.

What is checked

Each action is resolved against the type of dot at that point: field accesses, method calls (including on interface-typed values), registered template functions, and {{template}} calls with the type they pass along.

{{define "GET /user GetUser(ctx)"}}
<p>{{.Result.Email}}</p>  <!-- ok -->
<p>{{.Result.Phone}}</p>  <!-- error: User has no Phone field -->
{{end}}

Each error names the call site, the template position, then the type:

/home/me/app/template_routes.go:38:13 ExecuteTemplate "GET /user GetUser(ctx)" *TemplateData[RoutesReceiver, User]
/home/me/app/template.gohtml:3:13: executing "GET /user GetUser(ctx)" at <.Result.Phone>: field or method Phone not found on User

  type User struct {
    Email string
  }

Error: fail: 1 error

reference_check_types.txt · err_check_with_wrong_field.txt

Check also fails on:

What is not checked

Case Effect
Anything after the initializer (init, a later templates = ...) Not evaluated. Only the variable's initializer expression is read; a Funcs/ParseFS chain in init leaves muxt check reporting ok: 0 templates and muxt generate writing 0 routes.
GoLand gotype comments Not read.