6 "github.com/clipperhouse/typewriter"
9 var templates = typewriter.TemplateSlice{
14 // this is the template for generating the go-data wrappers of an interface
15 var wrapper = &typewriter.Template{
17 TypeConstraint: typewriter.Constraint{},
20 type {{.Wrapper}} struct {
21 {{.Inner}} "json:\"unwrap\""
24 var {{.Wrapper}}Mapper = data.NewMapper({{.Wrapper}}{})
26 func (h {{.Wrapper}}) MarshalJSON() ([]byte, error) {
27 return {{.Wrapper}}Mapper.ToJSON(h.{{.Inner}})
30 func (h *{{.Wrapper}}) UnmarshalJSON(data []byte) (err error) {
31 parsed, err := {{.Wrapper}}Mapper.FromJSON(data)
32 if err == nil && parsed != nil {
33 h.{{.Inner}} = parsed.({{.Inner}})
38 // Unwrap recovers the concrete interface safely (regardless of levels of embeds)
39 func (h {{.Wrapper}}) Unwrap() {{.Inner}} {
41 for wrap, ok := hi.({{.Wrapper}}); ok; wrap, ok = hi.({{.Wrapper}}) {
47 func (h {{.Wrapper}}) Empty() bool {
48 return h.{{.Inner}} == nil
51 /*** below are bindings for each implementation ***/
55 var register = &typewriter.Template{
57 TypeConstraint: typewriter.Constraint{},
61 {{.Wrapper}}Mapper.RegisterImplementation({{ if .Impl.Pointer }}&{{ end }}{{.Impl.Name}}{}, "{{.ImplType }}", 0x{{.Count}})
64 func (hi {{ if .Impl.Pointer }}*{{ end }}{{.Impl.Name}}) Wrap() {{.Wrapper}} {
65 return {{.Wrapper}}{hi}
70 var fmap = map[string]interface{}{
71 "ToLower": strings.ToLower,