github.com/ethersphere/bee/v2@v2.2.0/pkg/resolver/mock/resolver.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/resolver"
    11  	"github.com/ethersphere/bee/v2/pkg/resolver/client/ens"
    12  )
    13  
    14  // Assure mock Resolver implements the Resolver interface.
    15  var _ resolver.Interface = (*Resolver)(nil)
    16  
    17  // Resolver is the mock Resolver implementation.
    18  type Resolver struct {
    19  	IsClosed    bool
    20  	resolveFunc func(string) (resolver.Address, error)
    21  }
    22  
    23  // Option function sets the option on the mock Resolver.
    24  type Option func(*Resolver)
    25  
    26  // NewResolver will create a new mock Resolver.
    27  func NewResolver(opts ...Option) resolver.Interface {
    28  	r := &Resolver{}
    29  
    30  	// Apply all options.
    31  	for _, o := range opts {
    32  		o(r)
    33  	}
    34  
    35  	return r
    36  }
    37  
    38  // WithResolveFunc will override the Resolve function implementation.
    39  func WithResolveFunc(f func(string) (resolver.Address, error)) Option {
    40  	return func(r *Resolver) {
    41  		r.resolveFunc = f
    42  	}
    43  }
    44  
    45  // Resolve implements the Resolver interface.
    46  func (r *Resolver) Resolve(name string) (resolver.Address, error) {
    47  	if r.resolveFunc != nil {
    48  		return r.resolveFunc(name)
    49  	}
    50  	return resolver.Address{}, fmt.Errorf("resolveFunc: %w", ens.ErrNotImplemented)
    51  }
    52  
    53  // Close implements the Resolver interface.
    54  func (r *Resolver) Close() error {
    55  	r.IsClosed = true
    56  
    57  	return nil
    58  }