[#625] morph/client: make method names constant

We don't use custom names and the only place where custom method option
is used it provides the default name and can be omitted.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-29 16:06:36 +03:00 committed by LeL
parent 35dec2f494
commit 3c5b62d839
44 changed files with 249 additions and 1097 deletions

View file

@ -14,120 +14,27 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static reputation contract client
*cfg // contract method names
}
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
putMethod,
getMethod,
getByIDMethod,
listByEpochMethod string
}
const (
defaultPutMethod = "put"
defaultGetMethod = "get"
defaultGetByIDMethod = "getByID"
defaultListByEpochMethod = "listByEpoch"
putMethod = "put"
getMethod = "get"
getByIDMethod = "getByID"
listByEpochMethod = "listByEpoch"
)
func defaultConfig() *cfg {
return &cfg{
putMethod: defaultPutMethod,
getMethod: defaultGetMethod,
getByIDMethod: defaultGetByIDMethod,
listByEpochMethod: defaultListByEpochMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
return &Client{client: c}, nil
}
// Morph returns raw morph client.
func (c Client) Morph() *client.Client {
return c.client.Morph()
}
// WithPutMethod returns a client constructor option that
// specifies the method name to put reputation value.
//
// Ignores empty value.
//
// If option not provided, "put" is used.
func WithPutMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putMethod = n
}
}
}
// WithGetMethod returns a client constructor option that
// specifies the method name to get reputation value.
//
// Ignores empty value.
//
// If option not provided, "get" is used.
func WithGetMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getMethod = n
}
}
}
// WithGetByIDMethod returns a client constructor option that
// specifies the method name to get reputation value by it's ID in the contract.
//
// Ignores empty value.
//
// If option not provided, "getByID" is used.
func WithGetByIDMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getByIDMethod = n
}
}
}
// WithListByEpochMethod returns a client constructor option that
// specifies the method name to list reputation value IDs for certain epoch.
//
// Ignores empty value.
//
// If option not provided, "listByEpoch" is used.
func WithListByEpochDMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listByEpochMethod = n
}
}
}