github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/decoder/adapters.go (about)

     1  package decoder
     2  
     3  import (
     4  	"github.com/abemedia/go-don/internal/byteconv"
     5  	"github.com/abemedia/httprouter"
     6  	"github.com/valyala/fasthttp"
     7  )
     8  
     9  type Map map[string][]string
    10  
    11  func (m Map) Get(key string) string {
    12  	if m == nil {
    13  		return ""
    14  	}
    15  	if vs := m[key]; len(vs) > 0 {
    16  		return vs[0]
    17  	}
    18  	return ""
    19  }
    20  
    21  func (m Map) Values(key string) []string {
    22  	if m == nil {
    23  		return nil
    24  	}
    25  	return m[key]
    26  }
    27  
    28  type Args fasthttp.Args
    29  
    30  func (ps *Args) Get(key string) string {
    31  	return byteconv.Btoa((*fasthttp.Args)(ps).Peek(key))
    32  }
    33  
    34  func (ps *Args) Values(key string) []string {
    35  	args := (*fasthttp.Args)(ps).PeekMulti(key)
    36  	if len(args) == 0 {
    37  		return nil
    38  	}
    39  
    40  	res := make([]string, len(args))
    41  	for i := range args {
    42  		res[i] = byteconv.Btoa(args[i])
    43  	}
    44  
    45  	return res
    46  }
    47  
    48  type Header fasthttp.RequestHeader
    49  
    50  func (ps *Header) Get(key string) string {
    51  	return byteconv.Btoa((*fasthttp.RequestHeader)(ps).Peek(key))
    52  }
    53  
    54  func (ps *Header) Values(key string) []string {
    55  	args := (*fasthttp.RequestHeader)(ps).PeekAll(key)
    56  	if len(args) == 0 {
    57  		return nil
    58  	}
    59  
    60  	res := make([]string, len(args))
    61  	for i := range args {
    62  		res[i] = byteconv.Btoa(args[i])
    63  	}
    64  
    65  	return res
    66  }
    67  
    68  type Params httprouter.Params
    69  
    70  func (ps Params) Get(key string) string {
    71  	for i := range ps {
    72  		if ps[i].Key == key {
    73  			return ps[i].Value
    74  		}
    75  	}
    76  	return ""
    77  }
    78  
    79  func (ps Params) Values(key string) []string {
    80  	for i := range ps {
    81  		if ps[i].Key == key {
    82  			return []string{ps[i].Value}
    83  		}
    84  	}
    85  	return nil
    86  }