github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/chain/handler.go (about)

     1  package chain
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	constant "github.com/NpoolPlatform/chain-middleware/pkg/const"
     8  	chaincrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/chain"
     9  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    10  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/chain"
    11  
    12  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    13  
    14  	"github.com/google/uuid"
    15  )
    16  
    17  type Handler struct {
    18  	ID         *uint32
    19  	EntID      *uuid.UUID
    20  	ChainType  *string
    21  	Logo       *string
    22  	ChainID    *string
    23  	NativeUnit *string
    24  	AtomicUnit *string
    25  	UnitExp    *uint32
    26  	ENV        *string
    27  	GasType    *basetypes.GasType
    28  	Nickname   *string
    29  	Conds      *chaincrud.Conds
    30  	Offset     int32
    31  	Limit      int32
    32  }
    33  
    34  func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) {
    35  	handler := &Handler{}
    36  	for _, opt := range options {
    37  		if err := opt(ctx, handler); err != nil {
    38  			return nil, err
    39  		}
    40  	}
    41  	return handler, nil
    42  }
    43  
    44  func WithID(u *uint32, must bool) func(context.Context, *Handler) error {
    45  	return func(ctx context.Context, h *Handler) error {
    46  		if u == nil {
    47  			if must {
    48  				return fmt.Errorf("invalid id")
    49  			}
    50  			return nil
    51  		}
    52  		h.ID = u
    53  		return nil
    54  	}
    55  }
    56  
    57  func WithEntID(id *string, must bool) func(context.Context, *Handler) error {
    58  	return func(ctx context.Context, h *Handler) error {
    59  		if id == nil {
    60  			if must {
    61  				return fmt.Errorf("invalid entid")
    62  			}
    63  			return nil
    64  		}
    65  		_id, err := uuid.Parse(*id)
    66  		if err != nil {
    67  			return err
    68  		}
    69  		h.EntID = &_id
    70  		return nil
    71  	}
    72  }
    73  
    74  func WithChainType(chainType *string, must bool) func(context.Context, *Handler) error {
    75  	return func(ctx context.Context, h *Handler) error {
    76  		if chainType == nil {
    77  			if must {
    78  				return fmt.Errorf("invalid chaintype")
    79  			}
    80  			return nil
    81  		}
    82  		if *chainType == "" {
    83  			return fmt.Errorf("invalid chaintype")
    84  		}
    85  		h.ChainType = chainType
    86  		return nil
    87  	}
    88  }
    89  
    90  func WithLogo(logo *string, must bool) func(context.Context, *Handler) error {
    91  	return func(ctx context.Context, h *Handler) error {
    92  		h.Logo = logo
    93  		return nil
    94  	}
    95  }
    96  
    97  func WithChainID(id *string, must bool) func(context.Context, *Handler) error {
    98  	return func(ctx context.Context, h *Handler) error {
    99  		if id == nil {
   100  			if must {
   101  				return fmt.Errorf("invalid chainid")
   102  			}
   103  			return nil
   104  		}
   105  		if *id == "" {
   106  			return fmt.Errorf("invalid chainid")
   107  		}
   108  		h.ChainID = id
   109  		return nil
   110  	}
   111  }
   112  
   113  func WithNativeUnit(unit *string, must bool) func(context.Context, *Handler) error {
   114  	return func(ctx context.Context, h *Handler) error {
   115  		if unit == nil {
   116  			if must {
   117  				return fmt.Errorf("invalid nativeunit")
   118  			}
   119  			return nil
   120  		}
   121  		if *unit == "" {
   122  			return fmt.Errorf("invalid nativeunit")
   123  		}
   124  		h.NativeUnit = unit
   125  		return nil
   126  	}
   127  }
   128  
   129  func WithAtomicUnit(unit *string, must bool) func(context.Context, *Handler) error {
   130  	return func(ctx context.Context, h *Handler) error {
   131  		if unit == nil {
   132  			if must {
   133  				return fmt.Errorf("invalid atomicunit")
   134  			}
   135  			return nil
   136  		}
   137  		if *unit == "" {
   138  			return fmt.Errorf("invalid atomicunit")
   139  		}
   140  		h.AtomicUnit = unit
   141  		return nil
   142  	}
   143  }
   144  
   145  func WithUnitExp(exp *uint32, must bool) func(context.Context, *Handler) error {
   146  	return func(ctx context.Context, h *Handler) error {
   147  		h.UnitExp = exp
   148  		return nil
   149  	}
   150  }
   151  
   152  func WithENV(env *string, must bool) func(context.Context, *Handler) error {
   153  	return func(ctx context.Context, h *Handler) error {
   154  		if env == nil {
   155  			if must {
   156  				return fmt.Errorf("invalid ent")
   157  			}
   158  			return nil
   159  		}
   160  		switch *env {
   161  		case "main":
   162  		case "test":
   163  		case "local":
   164  		default:
   165  			return fmt.Errorf("invalid coinenv")
   166  		}
   167  		h.ENV = env
   168  		return nil
   169  	}
   170  }
   171  
   172  func WithNickname(nickname *string, must bool) func(context.Context, *Handler) error {
   173  	return func(ctx context.Context, h *Handler) error {
   174  		if nickname == nil {
   175  			if must {
   176  				return fmt.Errorf("invalid nickname")
   177  			}
   178  			return nil
   179  		}
   180  		if *nickname == "" {
   181  			return fmt.Errorf("invalid nickname")
   182  		}
   183  		h.Nickname = nickname
   184  		return nil
   185  	}
   186  }
   187  
   188  func WithGasType(gasType *basetypes.GasType, must bool) func(context.Context, *Handler) error {
   189  	return func(ctx context.Context, h *Handler) error {
   190  		if gasType == nil {
   191  			if must {
   192  				return fmt.Errorf("invalid gastype")
   193  			}
   194  			return nil
   195  		}
   196  		switch *gasType {
   197  		case basetypes.GasType_FixedGas:
   198  		case basetypes.GasType_DynamicGas:
   199  		case basetypes.GasType_GasUnsupported:
   200  		default:
   201  			return fmt.Errorf("invalid gastype")
   202  		}
   203  		h.GasType = gasType
   204  		return nil
   205  	}
   206  }
   207  
   208  func WithConds(conds *npool.Conds) func(context.Context, *Handler) error {
   209  	return func(ctx context.Context, h *Handler) error {
   210  		h.Conds = &chaincrud.Conds{}
   211  		if conds == nil {
   212  			return nil
   213  		}
   214  		if conds.EntID != nil {
   215  			id, err := uuid.Parse(conds.GetEntID().GetValue())
   216  			if err != nil {
   217  				return err
   218  			}
   219  			h.Conds.EntID = &cruder.Cond{
   220  				Op:  conds.GetEntID().GetOp(),
   221  				Val: id,
   222  			}
   223  		}
   224  		if conds.ENV != nil {
   225  			h.Conds.ENV = &cruder.Cond{
   226  				Op:  conds.GetENV().GetOp(),
   227  				Val: conds.GetENV().GetValue(),
   228  			}
   229  		}
   230  		if conds.EntIDs != nil {
   231  			ids := []uuid.UUID{}
   232  			for _, id := range conds.GetEntIDs().GetValue() {
   233  				_id, err := uuid.Parse(id)
   234  				if err != nil {
   235  					return err
   236  				}
   237  				ids = append(ids, _id)
   238  			}
   239  			h.Conds.EntIDs = &cruder.Cond{
   240  				Op:  conds.GetEntIDs().GetOp(),
   241  				Val: ids,
   242  			}
   243  		}
   244  		if conds.ChainType != nil {
   245  			h.Conds.Name = &cruder.Cond{
   246  				Op:  conds.GetChainType().GetOp(),
   247  				Val: conds.GetChainType().GetValue(),
   248  			}
   249  		}
   250  		return nil
   251  	}
   252  }
   253  
   254  func WithOffset(offset int32) func(context.Context, *Handler) error {
   255  	return func(ctx context.Context, h *Handler) error {
   256  		h.Offset = offset
   257  		return nil
   258  	}
   259  }
   260  
   261  func WithLimit(limit int32) func(context.Context, *Handler) error {
   262  	return func(ctx context.Context, h *Handler) error {
   263  		if limit == 0 {
   264  			limit = constant.DefaultRowLimit
   265  		}
   266  		h.Limit = limit
   267  		return nil
   268  	}
   269  }