upd(codegen): typeset updates with Resources()

This commit is contained in:
Tit Petric
2019-02-05 14:41:57 +01:00
parent 675b52e642
commit 13a11ab3b4
2 changed files with 72 additions and 37 deletions
+12 -9
View File
@@ -22,9 +22,9 @@ function permissions {
CGO_ENABLED=0 go build -o ./build/gen-permissions codegen/v2/permissions.go
fi
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rules.OperationGroup" -input sam/types/permissions/1-organisation.json -output sam/types/organisation.perms.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rules.OperationGroup" -input sam/types/permissions/2-team.json -output sam/types/team.perms.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rules.OperationGroup" -input sam/types/permissions/3-channel.json -output sam/types/channel.perms.go
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rules.OperationGroup" -input sam/types/permissions/1-organisation.json -output sam/types/organisation.perms.gen.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rules.OperationGroup" -input sam/types/permissions/2-team.json -output sam/types/team.perms.gen.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rules.OperationGroup" -input sam/types/permissions/3-channel.json -output sam/types/channel.perms.gen.go
green "OK"
}
@@ -38,19 +38,22 @@ function types {
fi
./build/gen-type-set --types Module,Page,Chart,Trigger,Record \
--no-pk-types ModuleField,RecordValue \
--output crm/types/type.gen.go
--output crm/types/type.primary.gen.go
./build/gen-type-set --with-primary-key=false --types ModuleField,RecordValue \
--output crm/types/type.other.gen.go
./build/gen-type-set --types MessageAttachment --output sam/types/attachment.gen.go
./build/gen-type-set --types Channel --output sam/types/channel.gen.go
./build/gen-type-set --no-pk-types ChannelMember --output sam/types/channel_member.gen.go
./build/gen-type-set --no-pk-types Command,CommandParam --output sam/types/command.gen.go
./build/gen-type-set --with-resources=true --types Channel --resource-type "types.Resource" --imports "github.com/crusttech/crust/system/types" --output sam/types/channel.gen.go
./build/gen-type-set --with-primary-key=false --types ChannelMember --output sam/types/channel_member.gen.go
./build/gen-type-set --with-primary-key=false --types Command,CommandParam --output sam/types/command.gen.go
./build/gen-type-set --types Mention --output sam/types/mention.gen.go
./build/gen-type-set --types MessageFlag --output sam/types/message_flag.gen.go
./build/gen-type-set --types Message --output sam/types/message.gen.go
./build/gen-type-set --no-pk-types Unread --output sam/types/unread.gen.go
./build/gen-type-set --with-primary-key=false --types Unread --output sam/types/unread.gen.go
./build/gen-type-set --types User --output system/types/user.gen.go
./build/gen-type-set --with-resources=true --resource-type "Resource" --types Team --output system/types/team.gen.go
./build/gen-type-set --with-resources=true --resource-type "Resource" --types Organisation --output system/types/organisation.gen.go
./build/gen-type-set --types Credentials --output system/types/credentials.gen.go
green "OK"
}
+60 -28
View File
@@ -15,22 +15,30 @@ import (
const tmplTypeSet = `package types
{{ if .Imports }}
import (
{{ range $i, $import := .Imports }}
"{{ $import }}"
{{ end }}
)
{{ end }}
// Hello! This file is auto-generated.
type (
{{ range $i, $set := .Sets }}
// {{ $set.Name }}Set slice of {{ $set.Name }}
// {{ $set }}Set slice of {{ $set }}
//
// This type is auto-generated.
{{ $set.Name }}Set []*{{ $set.Name }}
{{ $set }}Set []*{{ $set }}
{{ end }}
)
{{ range $i, $set := .Sets }}
// Walk iterates through every slice item and calls w({{ $set.Name }}) err
// Walk iterates through every slice item and calls w({{ $set }}) err
//
// This function is auto-generated.
func (set {{ $set.Name }}Set) Walk(w func(*{{ $set.Name }}) error) (err error) {
func (set {{ $set }}Set) Walk(w func(*{{ $set }}) error) (err error) {
for i := range set {
if err = w(set[i]); err != nil {
return
@@ -40,12 +48,12 @@ func (set {{ $set.Name }}Set) Walk(w func(*{{ $set.Name }}) error) (err error) {
return
}
// Filter iterates through every slice item, calls f({{ $set.Name }}) (bool, err) and return filtered slice
// Filter iterates through every slice item, calls f({{ $set }}) (bool, err) and return filtered slice
//
// This function is auto-generated.
func (set {{ $set.Name }}Set) Filter(f func(*{{ $set.Name }}) (bool, error)) (out {{ $set.Name }}Set, err error) {
func (set {{ $set }}Set) Filter(f func(*{{ $set }}) (bool, error)) (out {{ $set }}Set, err error) {
var ok bool
out = {{ $set.Name }}Set{}
out = {{ $set }}Set{}
for i := range set {
if ok, err = f(set[i]); err != nil {
return
@@ -57,11 +65,11 @@ func (set {{ $set.Name }}Set) Filter(f func(*{{ $set.Name }}) (bool, error)) (ou
return
}
{{ if $set.PK }}
{{ if $.WithPrimaryKey }}
// FindByID finds items from slice by its ID property
//
// This function is auto-generated.
func (set {{ $set.Name }}Set) FindByID(ID uint64) *{{ $set.Name }} {
func (set {{ $set }}Set) FindByID(ID uint64) *{{ $set }} {
for i := range set {
if set[i].ID == ID {
return set[i]
@@ -74,7 +82,7 @@ func (set {{ $set.Name }}Set) FindByID(ID uint64) *{{ $set.Name }} {
// IDs returns a slice of uint64s from all items in the set
//
// This function is auto-generated.
func (set {{ $set.Name }}Set) IDs() (IDs []uint64) {
func (set {{ $set }}Set) IDs() (IDs []uint64) {
IDs = make([]uint64, len(set))
for i := range set {
@@ -83,6 +91,23 @@ func (set {{ $set.Name }}Set) IDs() (IDs []uint64) {
return
}
{{ end }}
{{ if $.WithResources }}
// Resources returns a slice of types.Resource from all items in the set
//
// This function is auto-generated.
func (set {{ $set }}Set) Resources() (Resources []{{ $.ResourceType }}) {
Resources = make([]{{ $.ResourceType }}, len(set))
for i := range set {
Resources[i] = set[i].Resource()
}
return
}
{{ end }}
{{ end }}
`
@@ -92,36 +117,43 @@ func main() {
tpl := template.Must(template.Must(t.Clone()).Parse(tmplTypeSet))
type (
set struct {
Name string
PK bool
}
)
var (
stdTypesStr, nopkTypesStr, outputFile string
output io.Writer
importsStr,
stdTypesStr,
outputFile string
output io.Writer
)
payload := struct {
Sets []string
Imports []string
ResourceType string
WithPrimaryKey bool
WithResources bool
}{}
flag.StringVar(&stdTypesStr, "types", "", "Comma separated list of types")
flag.StringVar(&nopkTypesStr, "no-pk-types", "", "Comma separated list of types without ID field")
flag.StringVar(&importsStr, "imports", "", "Comma separated list of imports")
flag.BoolVar(&payload.WithPrimaryKey, "with-primary-key", true, "Generate types with ID field")
flag.BoolVar(&payload.WithResources, "with-resources", false, "Generate Resources() functions")
flag.StringVar(&payload.ResourceType, "resource-type", "Resource", "Resource type name to use (with imports)")
flag.StringVar(&outputFile, "output", "", "JWT Expiration in minutes")
flag.Parse()
payload := struct {
Sets []set
}{}
for _, name := range strings.Split(stdTypesStr, ",") {
for _, name := range strings.Split(importsStr, ",") {
if name = strings.TrimSpace(name); len(name) > 0 {
payload.Sets = append(payload.Sets, set{Name: name, PK: true})
payload.Imports = append(payload.Imports, name)
}
}
for _, name := range strings.Split(nopkTypesStr, ",") {
for _, name := range strings.Split(stdTypesStr, ",") {
if name = strings.TrimSpace(name); len(name) > 0 {
payload.Sets = append(payload.Sets, set{Name: name, PK: false})
payload.Sets = append(payload.Sets, name)
}
}