From: Andrea Zagli Date: Sun, 22 Aug 2021 12:28:01 +0000 (+0200) Subject: Packaged as module. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1be0bcd9609a5d09885f5c9559125c9e4c05d166;p=zakgrid_go Packaged as module. --- diff --git a/go.mod b/go.mod index 0ac1ed5..d1b8964 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module examples.com/zakgrid_go go 1.16 -require github.com/mattn/go-sqlite3 v1.14.8 +require ( + example.com/zakgrid/grid v0.0.0-00010101000000-000000000000 + github.com/mattn/go-sqlite3 v1.14.8 +) + +replace example.com/zakgrid/grid => ./grid diff --git a/grid/go.mod b/grid/go.mod new file mode 100644 index 0000000..9a7bc1c --- /dev/null +++ b/grid/go.mod @@ -0,0 +1,3 @@ +module example.com/zakgrid/grid + +go 1.16 diff --git a/grid/grid.go b/grid/grid.go new file mode 100644 index 0000000..11ce747 --- /dev/null +++ b/grid/grid.go @@ -0,0 +1,134 @@ +package zakgrid + +import ( + "fmt" + "bytes" + "text/template" +) + +type MissingCol func (map[string]interface{}) interface{} + +type Column struct { + Name string + Title string + Align int + ToCall MissingCol +} + +var headerTmpl = "\n{{ . }}\n" +var headerRowTmpl = "\t{{ .Title }}\n" + +var rowTmpl = "\n{{ . }}\n" +var rowColTmpl = "\t{{ .Value }}\n" + +type Grid struct { + cols []Column + HeaderTmpl string + HeaderRowTmpl string + RowTmpl string + RowColTmpl string +} + +func (g *Grid) AddColumn (c Column) { + g.cols = append(g.cols, c) +} + +func (g *Grid) GetHeader () string { + htmpl := g.HeaderTmpl + if htmpl == "" { + htmpl = headerTmpl + } + + t, err := template.New("header").Parse(htmpl) + if err != nil { + fmt.Println(err) + return "" + } + + hrtmpl := g.HeaderRowTmpl + if hrtmpl == "" { + hrtmpl = headerRowTmpl + } + + tr, err := template.New("headerRow").Parse(hrtmpl) + if err != nil { + fmt.Println(err) + return "" + } + + header := "" + + for i, v := range g.cols { + var b bytes.Buffer + err = tr.Execute(&b, v) + + header += b.String() + fmt.Println(i, v) + } + + var b bytes.Buffer + err = t.Execute(&b, header) + if err != nil { + fmt.Println(err) + return "" + } + + return b.String() +} + +func (g *Grid) GetRowFromMap (m map[string]interface{}) string { + type nc struct { + Column + Value interface{} + } + + rtmpl := g.RowTmpl + if rtmpl == "" { + rtmpl = rowTmpl + } + + t, err := template.New("row").Parse(rtmpl) + if err != nil { + fmt.Println(err) + return "" + } + + rctmpl := g.RowColTmpl + if rctmpl == "" { + rctmpl = rowColTmpl + } + + tc, err := template.New("rowCol").Parse(rctmpl) + if err != nil { + fmt.Println(err) + return "" + } + + ret := "" + + for _, c := range g.cols { + v, ok := m[c.Name] + if !ok { + if c.ToCall != nil { + v = c.ToCall(m) + } + } + + nnc := nc{Column: c, Value: v} + fmt.Println(nnc) + + var b bytes.Buffer + err = tc.Execute(&b, nnc) + + ret += b.String() + } + + var b bytes.Buffer + err = t.Execute(&b, ret) + if err != nil { + fmt.Println(err) + return "" + } + + return b.String() +} diff --git a/main.go b/main.go index 844585b..b8c58e7 100644 --- a/main.go +++ b/main.go @@ -3,139 +3,12 @@ package main import ( "fmt" "log" - "bytes" - "text/template" "database/sql" _ "github.com/mattn/go-sqlite3" -) - -type missingCol func (map[string]interface{}) interface{} - -type Column struct { - Name string - Title string - Align int - toCall missingCol -} - -var headerTmpl = "\n{{ . }}\n" -var headerRowTmpl = "\t{{ .Title }}\n" - -var rowTmpl = "\n{{ . }}\n" -var rowColTmpl = "\t{{ .Value }}\n" - -type grid struct { - cols []Column - headerTmpl string - headerRowTmpl string - rowTmpl string - rowColTmpl string -} - -func (g *grid) addColumn (c Column) { - g.cols = append(g.cols, c) -} - -func (g *grid) getHeader () string { - htmpl := g.headerTmpl - if htmpl == "" { - htmpl = headerTmpl - } - - t, err := template.New("header").Parse(htmpl) - if err != nil { - fmt.Println(err) - return "" - } - - hrtmpl := g.headerRowTmpl - if hrtmpl == "" { - hrtmpl = headerRowTmpl - } - - tr, err := template.New("headerRow").Parse(hrtmpl) - if err != nil { - fmt.Println(err) - return "" - } - - header := "" - - for i, v := range g.cols { - var b bytes.Buffer - err = tr.Execute(&b, v) - - header += b.String() - fmt.Println(i, v) - } - - var b bytes.Buffer - err = t.Execute(&b, header) - if err != nil { - fmt.Println(err) - return "" - } - return b.String() -} - -func (g *grid) getRowFromMap (m map[string]interface{}) string { - type nc struct { - Column - Value interface{} - } - - rtmpl := g.rowTmpl - if rtmpl == "" { - rtmpl = rowTmpl - } - - t, err := template.New("row").Parse(rtmpl) - if err != nil { - fmt.Println(err) - return "" - } - - rctmpl := g.rowColTmpl - if rctmpl == "" { - rctmpl = rowColTmpl - } - - tc, err := template.New("rowCol").Parse(rctmpl) - if err != nil { - fmt.Println(err) - return "" - } - - ret := "" - - for _, c := range g.cols { - v, ok := m[c.Name] - if !ok { - if c.toCall != nil { - v = c.toCall(m) - } - } - - nnc := nc{Column: c, Value: v} - fmt.Println(nnc) - - var b bytes.Buffer - err = tc.Execute(&b, nnc) - - ret += b.String() - } - - var b bytes.Buffer - err = t.Execute(&b, ret) - if err != nil { - fmt.Println(err) - return "" - } - - return b.String() -} + "example.com/zakgrid/grid" +) func missing (row map[string]interface{}) interface{} { ret := "" @@ -144,32 +17,32 @@ func missing (row map[string]interface{}) interface{} { } func main() { - var g grid + var g zakgrid.Grid - g.headerTmpl = "\n{{ . }}\n" + g.HeaderTmpl = "\n{{ . }}\n" - g.rowColTmpl = "\t{{ .Value }}\n" + g.RowColTmpl = "\t{{ .Value }}\n" - c := Column{Name: "id", Title: "ID"} - g.addColumn(c) + c := zakgrid.Column{Name: "id", Title: "ID"} + g.AddColumn(c) - c = Column{Name: "name", Title: "Name"} - g.addColumn(c) + c = zakgrid.Column{Name: "name", Title: "Name"} + g.AddColumn(c) - c = Column{Name: "surname", Title: "Surname"} - g.addColumn(c) + c = zakgrid.Column{Name: "surname", Title: "Surname"} + g.AddColumn(c) - c = Column{Name: "age", Title: "Age"} - g.addColumn(c) + c = zakgrid.Column{Name: "age", Title: "Age"} + g.AddColumn(c) - c = Column{Name: "btn", Title: "Buttons"} - c.toCall = missing + c = zakgrid.Column{Name: "btn", Title: "Buttons"} + c.ToCall = missing c.Align = 2 - g.addColumn(c) + g.AddColumn(c) fmt.Println (g) - header := g.getHeader() + header := g.GetHeader() fmt.Println(header) @@ -210,6 +83,6 @@ func main() { fmt.Println(m) - fmt.Println(g.getRowFromMap(m)) + fmt.Println(g.GetRowFromMap(m)) } }