github.com/lestrrat-go/jwx/v2@v2.0.21/jwt/internal/types/string.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/lestrrat-go/jwx/v2/internal/json"
     7  )
     8  
     9  type StringList []string
    10  
    11  func (l StringList) Get() []string {
    12  	return []string(l)
    13  }
    14  
    15  func (l *StringList) Accept(v interface{}) error {
    16  	switch x := v.(type) {
    17  	case string:
    18  		*l = StringList([]string{x})
    19  	case []string:
    20  		*l = StringList(x)
    21  	case []interface{}:
    22  		list := make(StringList, len(x))
    23  		for i, e := range x {
    24  			if s, ok := e.(string); ok {
    25  				list[i] = s
    26  				continue
    27  			}
    28  			return fmt.Errorf(`invalid list element type %T`, e)
    29  		}
    30  		*l = list
    31  	default:
    32  		return fmt.Errorf(`invalid type: %T`, v)
    33  	}
    34  	return nil
    35  }
    36  
    37  func (l *StringList) UnmarshalJSON(data []byte) error {
    38  	var v interface{}
    39  	if err := json.Unmarshal(data, &v); err != nil {
    40  		return fmt.Errorf(`failed to unmarshal data: %w`, err)
    41  	}
    42  	return l.Accept(v)
    43  }