github.com/aminjam/goflat@v0.4.1-0.20160331105230-ec639fc0d5b3/runtime.go (about)

     1  package goflat
     2  
     3  const (
     4  	MainGotempl = `package main
     5  import (
     6      "bytes"
     7      "fmt"
     8      "io/ioutil"
     9      "os"
    10      "text/template"
    11      )
    12  func checkError(err error, detail string) {
    13    if err != nil {
    14      fmt.Printf("Fatal error %s: %s ", detail, err.Error())
    15        os.Exit(1)
    16    }
    17  }
    18  func main() {
    19    data, err := ioutil.ReadFile("{{.GoTemplate}}")
    20      checkError(err, "reading template file")
    21      pipes := NewPipes()
    22      {{if ne .CustomPipes ""}}
    23      pipes.Extend(CustomPipes())
    24      {{end}}
    25      tmpl, err := template.New("").Funcs(pipes.Map).Parse(string(data))
    26      checkError(err, "parsing template file")
    27      var result struct {
    28        {{if gt (len .GoInputs) 0}}
    29        {{range .GoInputs}}
    30        {{.StructName}} {{.StructName}}
    31        {{end}}
    32        {{end}}
    33      }
    34    {{if gt (len .GoInputs) 0}}
    35    {{range .GoInputs}}
    36    result.{{.StructName}} = New{{.StructName}}()
    37    {{end}}
    38    {{end}}
    39    var output bytes.Buffer
    40      err = tmpl.Execute(&output, result)
    41      checkError(err, "executing template output")
    42      fmt.Println(string(output.Bytes()))
    43  }
    44  `
    45  	PipesGo = `package runtime
    46  
    47  import (
    48  	"reflect"
    49  	"strings"
    50  	"text/template"
    51  )
    52  
    53  type Pipes struct {
    54  	Map template.FuncMap
    55  }
    56  
    57  func (p *Pipes) Extend(fm template.FuncMap) {
    58  	for k, v := range fm {
    59  		p.Map[k] = v
    60  	}
    61  }
    62  
    63  func NewPipes() *Pipes {
    64  	return &Pipes{
    65  		Map: template.FuncMap{
    66  			"join": func(sep string, a []string) (string, error) {
    67  				return strings.Join(a, sep), nil
    68  			},
    69  			//e.g. map "Name,Age,Job" "|"  => "[John|25|Painter Jane|21|Teacher]"
    70  			"map": func(f, sep string, a interface{}) ([]string, error) {
    71  				fields := strings.Split(f, ",")
    72  				reflectedArray := reflect.ValueOf(a)
    73  				out := make([]string, reflectedArray.Len())
    74  				i := 0
    75  				for i < len(out) {
    76  					v := reflectedArray.Index(i)
    77  					row := make([]string, len(fields))
    78  					for k, field := range fields {
    79  						row[k] = v.FieldByName(field).String()
    80  					}
    81  					out[i] = strings.Join(row, sep)
    82  					i++
    83  				}
    84  				return out, nil
    85  			},
    86  			"replace": func(old, new, s string) (string, error) {
    87  				//replace all occurrences of a value
    88  				return strings.Replace(s, old, new, -1), nil
    89  			},
    90  			"split": func(sep, s string) ([]string, error) {
    91  				s = strings.TrimSpace(s)
    92  				if s == "" {
    93  					return []string{}, nil
    94  				}
    95  				return strings.Split(s, sep), nil
    96  			},
    97  			"toUpper": func(s string) (string, error) {
    98  				return strings.ToUpper(s), nil
    99  			},
   100  			"toLower": func(s string) (string, error) {
   101  				return strings.ToLower(s), nil
   102  			},
   103  		},
   104  	}
   105  }
   106  `
   107  )