From 58cf5fde9586e387c34a10c1a8c4cfef82d725b3 Mon Sep 17 00:00:00 2001 From: Peter Ng Date: Mon, 8 Oct 2018 21:46:06 -0400 Subject: [PATCH 1/2] Adds New function - Constructs a new loader given properties without exposing internal properties --- template.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/template.go b/template.go index e4e5324..90ee063 100644 --- a/template.go +++ b/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 From 2f5ab57d9282233d4e05e405d3290dc4ffbe2ee3 Mon Sep 17 00:00:00 2001 From: Peter Ng Date: Mon, 8 Oct 2018 22:15:02 -0400 Subject: [PATCH 2/2] Re-generated the loaders with `New` --- example/pkgname/userloader_gen.go | 21 +++++++++++++++++++++ example/slice/usersliceloader_gen.go | 21 +++++++++++++++++++++ example/userloader_gen.go | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+) 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