github.com/crosbymichael/octokat@v0.0.0-20160826194511-076a32289ed5/params.go (about)

     1  package octokat
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type Params map[string]interface{}
     9  
    10  func (p Params) Put(key string, value interface{}) interface{} {
    11  	v, ok := p[key]
    12  	p[key] = value
    13  
    14  	if ok {
    15  		return v
    16  	} else {
    17  		return nil
    18  	}
    19  }
    20  
    21  func (p Params) Delete(key string) interface{} {
    22  	v, ok := p[key]
    23  	if !ok {
    24  		return nil
    25  	}
    26  
    27  	delete(p, key)
    28  
    29  	return v
    30  }
    31  
    32  func (p Params) Size() int {
    33  	return len(p)
    34  }
    35  
    36  func (p Params) Require(fields ...string) error {
    37  	missingFields := []string{}
    38  
    39  	for _, field := range fields {
    40  		_, ok := p[field]
    41  		if !ok {
    42  			missingFields = append(missingFields, field)
    43  		}
    44  	}
    45  
    46  	if len(missingFields) > 0 {
    47  		return fmt.Errorf("Missing fields: %s", strings.Join(missingFields, ", "))
    48  	}
    49  
    50  	return nil
    51  }