Skip to content

Commit

Permalink
Add client Response.Data type that is not Storable.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchoi-viant committed Sep 18, 2023
1 parent fd554f5 commit 498222b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
12 changes: 12 additions & 0 deletions example/client/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

type customMakerRegistry struct {
registry map[string]func() interface{}
}

func (c *customMakerRegistry) Register(k string, gen func() interface{}) (old func() interface{}, ok bool) {
old, ok = c.registry[k]
c.registry[k] = gen

return old, ok
}
6 changes: 6 additions & 0 deletions example/client/mlyc/mlyc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ func main() {
return &segmenteds
})

// this is an example usage of a non-Storable type being bound to Response.Data
client.CustomMakerRegistry.Register("custom", func() interface{} {
// TODO provide actual example
return struct{}{}
})

client.Run(os.Args[1:])
}
5 changes: 3 additions & 2 deletions example/client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type Options struct {
Debug bool `long:"debug"`
TimeoutUs int `short:"t" long:"timeout"`

Model string `short:"m" long:"model" description:"model"`
Storable string `short:"s" long:"storable"`
Model string `short:"m" long:"model" description:"model"`
Storable string `short:"s" long:"storable"`
CustomMaker string `long:"maker" description:"non-storable Response.Data, requires binding with CustomMaker"`

CacheMB int `long:"cache"`
NoHashCheck bool `long:"nohash"`
Expand Down
22 changes: 21 additions & 1 deletion example/client/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/viant/toolbox"
)

// Use CustomMakerRegistry with --maker to use a specific entity for Response.Data.
var CustomMakerRegistry *customMakerRegistry = new(customMakerRegistry)

func RunWithOptions(options *Options) error {
options.Init()
if err := options.Validate(); err != nil {
Expand Down Expand Up @@ -74,6 +77,8 @@ func RunWithOptions(options *Options) error {

response := &client.Response{}

var dataSetter func() interface{}

storableSrv := storable.Singleton()
maker, err := storableSrv.Lookup(options.Storable)
if err != nil {
Expand All @@ -84,7 +89,22 @@ func RunWithOptions(options *Options) error {
maker = checker.Generated(cli.Config.Datastore.MetaInput.Outputs, pl.Batch, false)
}

response.Data = maker()
dataSetter = func() interface{} {
return maker()
}

if options.CustomMaker != "" {
custom, ok := CustomMakerRegistry.registry[options.CustomMaker]
if !ok {
if options.Debug {
fmt.Printf("no such custom maker:\"%s\"\n", options.CustomMaker)
}
} else {
dataSetter = custom
}
}

response.Data = dataSetter()

ctx := context.Background()
cancel := func() {}
Expand Down

0 comments on commit 498222b

Please sign in to comment.