{"modulePath":"github.com/weiwolves/pkg","version":"v0.0.0-20200322065615-581c8d33cff6","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package tpl\n\n## Constants\n\n```go\nconst Copy = `// Copyright 2015-2017, Cyrill @ Schumacher.fm and the CoreStore contributors\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n//     http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// +build mage1 mage2\n\npackage {{ .Package }}\n`\n```\n\n```go\nconst EAValueStructure = `\n{{range $typeCode,$valueTables := .TypeCodeValueTables}}\n// Get{{ $typeCode | prepareVar }}ValueStructure returns for an EAV index the table structure.\n// Important also if you have custom value tables\nfunc Get{{ $typeCode | prepareVar }}ValueStructure(i eav.ValueIndex) (*csdb.Table, error) {\n\tswitch i {\n\t{{range $vt,$v := $valueTables }}case eav.EntityType{{ $v | prepareVar }}:\n\t\treturn TableCollection.Structure(TableIndex{{ $vt | prepareVar }})\n    {{end}}\t}\n\treturn nil, eav.ErrEntityTypeValueNotFound\n}\n{{end}}\n`\n```\n\n```go\nconst ExtractFromSlice = `\n// Extract{{.Name | camelize}} functions for extracting fields from {{.Name | camelize}}\n// slice. Generated via tableToStruct.\ntype Extract{{.Name | camelize}} struct {\n{{ range $k,$c := .Columns }} {{$c.Name | camelize }} func() []{{$c.GetGoPrimitive false}}\n{{end}} }\n\n// {{ typePrefix \"Extract\" }} extracts from a specified field all values into a slice.\n// Generated via tableToStruct.\nfunc (s {{$.Slice}}) {{ typePrefix \"Extract\" }}() Extract{{.Name | camelize}} {\n\treturn Extract{{.Name | camelize}} {\n\t\t{{ range $k,$c := .Columns }} {{$c.Name | camelize }} : func() []{{$c.GetGoPrimitive false}} {\n\t\t\text := make([]{{$c.GetGoPrimitive false}}, 0, len(s))\n\t\t\tfor _, v := range s {\n\t\t\t\text = append(ext, v.{{ $c.Name | camelize }}{{dbrType $c}})\n\t\t\t}\n\t\t\treturn ext\n\t\t},\n\t\t{{end}} }\n}\n`\n```\n\n```go\nconst FindBy = `\n{{if (.FindByPk) ne \"\"}}\n// {{ typePrefix .FindByPk }} searches the primary keys and returns a\n// *{{.Struct}} if found or nil and false.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix .FindByPk }}(\n{{range $k,$v := .Columns.PrimaryKeys}} {{ $v.Name }} {{$v.GetGoPrimitive false}},\n{{end}}\t) (match *{{.Struct}}, found bool) {\n\tfor _, u := range s {\n\t\tif u != nil {{ range $c := .Columns.PrimaryKeys }} \u0026\u0026 u.{{ $c.Name | camelize }}{{dbrType $c}} == {{$c.Name}} {{ end }} {\n\t\t\tmatch = u\n\t\t\tfound = true\n\t\t\treturn\n\t\t}\n\t}\n\treturn\n}\n{{end}}\n\n{{ range $k,$c := .Columns.UniqueKeys }}\n// {{ findBy $c.Name | typePrefix }} searches through this unique key and returns\n// a *{{$.Struct}} if found or nil and false.\n// Generated via tableToStruct.\nfunc (s {{$.Slice}}) {{ findBy $c.Name | typePrefix }} ( {{ $c.Name }} {{$c.GetGoPrimitive false}} ) (match *{{$.Struct}}, found bool) {\n\tfor _, u := range s {\n\t\tif u != nil \u0026\u0026 u.{{ $c.Name | camelize }}{{ dbrType $c }} == {{$c.Name}} {\n\t\t\tmatch = u\n\t\t\tfound = true\n\t\t\treturn\n\t\t}\n\t}\n\treturn\n}\n{{ end }}\n`\n```\n\n```go\nconst Header = `\n// Auto generated via tableToStruct\n\nimport (\n\t\"sort\"\n    {{ if .HasTypeCodeValueTables }}\n\t\"github.com/weiwolves/pkg/eav\"{{end}}\n\t\"github.com/weiwolves/pkg/storage/csdb\"\n\t\"github.com/weiwolves/pkg/storage/dbr\"\n)\n\n// TableIndex... is the index to a table. These constants are guaranteed\n// to stay the same for all Magento versions. Please access a table via this\n// constant instead of the raw table name. TableIndex iotas must start with 0.\nconst (\n    {{ range $k,$v := .Tables }}TableIndex{{$v.Name}} {{ if eq $k 0 }}csdb.Index = iota{{ end }} // Table: {{$v.TableName}}\n{{ end }}\tTableIndexZZZ  // the maximum index, which is not available.\n)\n\nfunc init(){\n    TableCollection = csdb.MustNewTableService(\n    {{ range $k,$v := .Tables }} csdb.WithTable(\n    \tTableIndex{{.Name}},\n    \t\"{{.TableName}}\",\n    \t{{.Columns}},\n    ),\n    {{ end }} )\n}`\n```\n\n```go\nconst SQL = `\n// {{ typePrefix \"SQLSelect\" }} fills this slice with data from the database.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"SQLSelect\" }}(dbrSess dbr.SessionRunner, cbs ...dbr.SelectCb) (int, error) {\n\treturn csdb.Load(dbrSess, TableCollection, TableIndex{{.Name}}, \u0026(*s), cbs...)\n}\n\n// {{ typePrefix \"SQLInsert\" }} inserts all records into the database @todo.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"SQLInsert\" }}(dbrSess dbr.SessionRunner, cbs ...dbr.InsertCb) (int, error) {\n\treturn 0, nil\n}\n\n// {{ typePrefix \"SQLUpdate\" }} updates all record in the database @todo.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"SQLUpdate\" }}(dbrSess dbr.SessionRunner, cbs ...dbr.UpdateCb) (int, error) {\n\treturn 0, nil\n}\n\n// {{ typePrefix \"SQLDelete\" }} deletes all record from the database @todo.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"SQLDelete\" }}(dbrSess dbr.SessionRunner, cbs ...dbr.DeleteCb) (int, error) {\n\treturn 0, nil\n}\n`\n```\n\n```go\nconst SliceFunctions = `// {{ typePrefix \"FilterThis\" }} filters the current slice by predicate f without memory allocation.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"FilterThis\" }} (f func(*{{.Struct}}) bool) {{.Slice}} {\n\tb := s[:0]\n\tfor _, x := range s {\n\t\tif f(x) {\n\t\t\tb = append(b, x)\n\t\t}\n\t}\n\treturn b\n}\n\n// {{ typePrefix \"Filter\" }} returns a new slice filtered by predicate f.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"Filter\" }} (f func(*{{.Struct}}) bool) {{.Slice}} {\n\tsl := make({{.Slice}}, 0, len(s))\n\tfor _, w := range s {\n\t\tif f(w) {\n\t\t\tsl = append(sl, w)\n\t\t}\n\t}\n\treturn sl\n}\n\n// {{ typePrefix \"FilterNot\" }} will return a new {{.Slice}} that does not match\n// by calling the function f\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"FilterNot\" }}(f func(*{{.Struct}}) bool) {{.Slice}} {\n\tsl := make({{.Slice}}, 0, len(s))\n\tfor _, v := range s {\n\t\tif f(v) == false {\n\t\t\tsl = append(sl, v)\n\t\t}\n\t}\n\treturn sl\n}\n\n// {{ typePrefix \"Each\" }} will run function f on all items in {{.Slice}}.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) Each(f func(*{{.Struct}}) ) {{.Slice}} {\n\tfor i := range s {\n\t\tf(s[i])\n\t}\n\treturn s\n}\n\n// {{ typePrefix \"Cut\" }} will remove items i through j-1.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"Cut\" }}(i, j int) {\n\tz := *s // copy slice header\n\tcopy(z[i:], z[j:])\n\tfor k, n := len(z)-j+i, len(z); k \u003c n; k++ {\n\t\tz[k] = nil // this should avoid the memory leak\n\t}\n\tz = z[:len(z)-j+i]\n\t*s = z\n}\n\n// {{ typePrefix \"Delete\" }} will remove an item from the slice.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"Delete\" }}(i int) {\n\tz := *s // copy the slice header\n\tend := len(z) - 1\n\ts.Swap(i, end)\n\tcopy(z[i:], z[i+1:])\n\tz[end] = nil // this should avoid the memory leak\n\tz = z[:end]\n\t*s = z\n}\n\n// {{ typePrefix \"Insert\" }} will place a new item at position i.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"Insert\" }}(n *{{.Struct}}, i int) {\n\tz := *s // copy the slice header\n\tz = append(z, \u0026{{.Struct}}{})\n\tcopy(z[i+1:], z[i:])\n\tz[i] = n\n\t*s = z\n}\n\n// {{ typePrefix \"Append\" }} will add a new item at the end of {{.Slice}}.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"Append\" }}(n ...*{{.Struct}}) {\n\t*s = append(*s, n...)\n}\n\n// {{ typePrefix \"Prepend\" }} will add a new item at the beginning of {{.Slice}}.\n// Generated via tableToStruct.\nfunc (s *{{.Slice}}) {{ typePrefix \"Prepend\" }}(n *{{.Struct}}) {\n\ts.Insert(n, 0)\n}\n`\n```\n\n```go\nconst Sort = `\n\ntype sort{{.Slice}} struct {\n\t{{.Slice}}\n\tlessFunc func(*{{.Struct}}, *{{.Struct}}) bool\n}\n\n// Less will satisfy the sort.Interface and compares via\n// the primary key.\n// Generated via tableToStruct.\nfunc (s sort{{.Slice}}) Less(i, j int) bool {\n\treturn s.lessFunc(s.{{.Slice}}[i], s.{{.Slice}}[j])\n}\n\n// {{ typePrefix \"Sort\" }} will sort {{.Slice}}.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"Sort\" }}(less func(*{{.Struct}}, *{{.Struct}}) bool) {\n\tsort.Sort(sort{{.Slice}} { s, less})\n}\n\n// {{ typePrefix \"Len\" }} returns the length and  will satisfy the sort.Interface.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"Len\" }}() int { return len(s) }\n\n// {{ typePrefix \"LessPK\" }} helper functions for sorting by ascending primary key.\n// Can be used as an argument in Sort().\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"LessPK\" }}(i, j *{{.Struct}}) bool {\n\treturn {{ range $c := .Columns.PrimaryKeys }} i.{{ $c.Name | camelize }}{{dbrType $c}} \u003c j.{{ $c.Name | camelize }}{{dbrType $c}} \u0026\u0026 {{ end }} 1 == 1\n}\n\n// {{ typePrefix \"Swap\" }} will satisfy the sort.Interface.\n// Generated via tableToStruct.\nfunc (s {{.Slice}}) {{ typePrefix \"Swap\" }}(i, j int) { s[i], s[j] = s[j], s[i] }\n`\n```\n\n```go\nconst StructFunctions = `\nfunc (et *TableEntityType) LoadByCode(dbrSess dbr.SessionRunner, code string, cbs ...dbr.SelectCb) error {\n\ts, err := TableCollection.Structure(TableIndexEntityType)\n\tif err != nil {\n\t\treturn errgo.Mask(err)\n\t}\n\n\trefactor like GetTables()\n\n\tsb := dbrSess.Select(s.AllColumnAliasQuote(csdb.MainTable)...).From(s.Name, csdb.MainTable).Where(\"entity_type_code = ?\", code)\n\tfor _, cb := range cbs {\n\t\tsb = cb(sb)\n\t}\n\treturn errgo.Mask(sb.LoadStruct(et))\n}\n`\n```\n\n```go\nconst Type = `\n// {{.Slice}} represents a collection type for DB table {{ .TableName }}\n// Generated via tableToStruct.\ntype {{.Slice}} []*{{.Struct}}\n\n// {{.Struct}} represents a type for DB table {{ .TableName }}\n// Generated via tableToStruct.\ntype {{.Struct}} struct {\n{{ range .GoColumns }}{{.GoName}} {{.GoType}} {{ $.Tick }}db:\"{{.Field.String}}\" json:\",omitempty\"{{ $.Tick }} {{.Comment}}\n{{ end }} }\n`\n```\n\n## Types\n\n```go\ntype Generics int\n```\nGenerics defines the available templates\n\n```go\nconst (\n\tOptSQL Generics = 1 \u003c\u003c iota\n\tOptFindBy\n\tOptSort\n\tOptSliceFunctions\n\tOptExtractFromSlice\n\tOptAll = OptSQL | OptFindBy | OptSort | OptSliceFunctions | OptExtractFromSlice\n)\n```\nOptions to be used to define which generic functions you need in a package.\n\n","path":"github.com/weiwolves/pkg/_codegen/tableToStruct/tpl","name":"tpl","synopsis":"","isRedistributable":true}
