common.go raw

   1  package apis
   2  
   3  import (
   4  	"fmt"
   5  
   6  	"github.com/mimuret/golang-iij-dpf/pkg/api"
   7  	"github.com/mimuret/golang-iij-dpf/pkg/utils"
   8  )
   9  
  10  type Params interface {
  11  	SetPathParams(...interface{}) error
  12  }
  13  
  14  // for ctl.
  15  func SetPathParams(args []interface{}, ids ...interface{}) error {
  16  	if len(args) == 0 {
  17  		return nil
  18  	}
  19  	if len(args) != len(ids) {
  20  		return fmt.Errorf("SetPathParams: args need %d items, but args len is %d", len(ids), len(args))
  21  	}
  22  	for i := range ids {
  23  		switch v := ids[i].(type) {
  24  		case *int64:
  25  			val, err := utils.ToInt64(args[i])
  26  			if err != nil {
  27  				return fmt.Errorf("failed to cast to int64 `%s`: %w", args[i], err)
  28  			}
  29  			*v = val
  30  		case *string:
  31  			val, err := utils.ToString(args[i])
  32  			if err != nil {
  33  				return fmt.Errorf("failed to cast to string `%s`: %w", args[i], err)
  34  			}
  35  			*v = val
  36  		default:
  37  			panic(fmt.Sprintf("ids[%d] is not int64 or string", i))
  38  		}
  39  	}
  40  	return nil
  41  }
  42  
  43  type Spec interface {
  44  	api.Spec
  45  	Params
  46  }
  47  
  48  type ListSpec interface {
  49  	api.ListSpec
  50  	Spec
  51  }
  52  
  53  type CountableListSpec interface {
  54  	api.CountableListSpec
  55  	Spec
  56  }
  57