diff --git a/example/pkgname/userloader_gen.go b/example/pkgname/userloader_gen.go index 23570b2..3cb61df 100644 --- a/example/pkgname/userloader_gen.go +++ b/example/pkgname/userloader_gen.go @@ -9,6 +9,27 @@ import ( "github.com/vektah/dataloaden/example" ) +// UserLoaderConfig captures the config to create a new UserLoader +type UserLoaderConfig struct { + // Fetch is a method that provides the data for the loader + Fetch func(keys []string) ([]*example.User, []error) + + // Wait is how long wait before sending a batch + Wait time.Duration + + // MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit + MaxBatch int +} + +// NewUserLoader creates a new UserLoader given a fetch, wait, and maxBatch +func NewUserLoader(config UserLoaderConfig) *UserLoader { + return &UserLoader{ + fetch: config.Fetch, + wait: config.Wait, + maxBatch: config.MaxBatch, + } +} + // UserLoader batches and caches requests type UserLoader struct { // this method provides the data for the loader diff --git a/example/slice/usersliceloader_gen.go b/example/slice/usersliceloader_gen.go index 1cb5142..08036d8 100644 --- a/example/slice/usersliceloader_gen.go +++ b/example/slice/usersliceloader_gen.go @@ -9,6 +9,27 @@ import ( "github.com/vektah/dataloaden/example" ) +// UserSliceLoaderConfig captures the config to create a new UserSliceLoader +type UserSliceLoaderConfig struct { + // Fetch is a method that provides the data for the loader + Fetch func(keys []int) ([][]example.User, []error) + + // Wait is how long wait before sending a batch + Wait time.Duration + + // MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit + MaxBatch int +} + +// NewUserSliceLoader creates a new UserSliceLoader given a fetch, wait, and maxBatch +func NewUserSliceLoader(config UserSliceLoaderConfig) *UserSliceLoader { + return &UserSliceLoader{ + fetch: config.Fetch, + wait: config.Wait, + maxBatch: config.MaxBatch, + } +} + // UserSliceLoader batches and caches requests type UserSliceLoader struct { // this method provides the data for the loader diff --git a/example/userloader_gen.go b/example/userloader_gen.go index d2759fe..26fc3aa 100644 --- a/example/userloader_gen.go +++ b/example/userloader_gen.go @@ -7,6 +7,27 @@ import ( "time" ) +// UserLoaderConfig captures the config to create a new UserLoader +type UserLoaderConfig struct { + // Fetch is a method that provides the data for the loader + Fetch func(keys []string) ([]*User, []error) + + // Wait is how long wait before sending a batch + Wait time.Duration + + // MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit + MaxBatch int +} + +// NewUserLoader creates a new UserLoader given a fetch, wait, and maxBatch +func NewUserLoader(config UserLoaderConfig) *UserLoader { + return &UserLoader{ + fetch: config.Fetch, + wait: config.Wait, + maxBatch: config.MaxBatch, + } +} + // UserLoader batches and caches requests type UserLoader struct { // this method provides the data for the loader diff --git a/pkg/generator/template.go b/pkg/generator/template.go index b4e283d..7efe375 100644 --- a/pkg/generator/template.go +++ b/pkg/generator/template.go @@ -6,14 +6,35 @@ var tpl = template.Must(template.New("generated").Parse(` // Code generated by github.com/vektah/dataloaden, DO NOT EDIT. package {{.Package}} - + import ( "sync" "time" - + {{if .Import}}"{{.Import}}"{{end}} ) +// {{.LoaderName}}Config captures the config to create a new {{.LoaderName}} +type {{.LoaderName}}Config struct { + // Fetch is a method that provides the data for the loader + Fetch func(keys []{{.KeyType}}) ([]{{.ValType}}, []error) + + // Wait is how long wait before sending a batch + Wait time.Duration + + // MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit + MaxBatch int +} + +// New{{.LoaderName}} creates a new {{.LoaderName}} given a fetch, wait, and maxBatch +func New{{.LoaderName}}(config {{.LoaderName}}Config) *{{.LoaderName}} { + return &{{.LoaderName}}{ + fetch: config.Fetch, + wait: config.Wait, + maxBatch: config.MaxBatch, + } +} + // {{.LoaderName}} batches and caches requests type {{.LoaderName}} struct { // this method provides the data for the loader