github.com/theliebeskind/genfig@v0.1.5-alpha/plugins/map.go (about)

     1  package plugins
     2  
     3  import (
     4  	"io"
     5  	"text/template"
     6  
     7  	"github.com/theliebeskind/genfig/models"
     8  )
     9  
    10  type mapPlugin struct {
    11  	s   models.SchemaMap
    12  	tpl *template.Template
    13  }
    14  
    15  var (
    16  	mapt = mapPlugin{
    17  		tpl: template.Must(template.
    18  			New("map").
    19  			Parse(`func (c *Config) Map() map[string]interface{} {
    20  	return c._map
    21  }
    22  `))}
    23  )
    24  
    25  func init() {
    26  	// "register" plugin
    27  	Plugins["map"] = &mapt
    28  }
    29  
    30  // GetInitCall returns the availibility and the string of the
    31  // function to be called on init
    32  func (p *mapPlugin) GetInitCall() (string, bool) {
    33  	return "", false
    34  }
    35  
    36  // SetSchemaMap sets the schema to be used when WriteTo is called
    37  func (p *mapPlugin) SetSchemaMap(s models.SchemaMap) {
    38  	p.s = s
    39  }
    40  
    41  // WriteTo performs the acutal writing to a buffer (or io.Writer).
    42  // For this plugin, the template is simply "rendered" into the writer.
    43  func (p *mapPlugin) WriteTo(w io.Writer) (l int64, err error) {
    44  	err = p.tpl.Execute(w, nil)
    45  	return
    46  }