...

Source file src/github.com/lestrrat-go/jwx/jwa/key_type_gen.go

Documentation: github.com/lestrrat-go/jwx/jwa

     1  // this file was auto-generated by internal/cmd/gentypes/main.go: DO NOT EDIT
     2  
     3  package jwa
     4  
     5  import (
     6  	"fmt"
     7  	"sort"
     8  	"sync"
     9  
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // KeyType represents the key type ("kty") that are supported
    14  type KeyType string
    15  
    16  // Supported values for KeyType
    17  const (
    18  	EC             KeyType = "EC"  // Elliptic Curve
    19  	InvalidKeyType KeyType = ""    // Invalid KeyType
    20  	OKP            KeyType = "OKP" // Octet string key pairs
    21  	OctetSeq       KeyType = "oct" // Octet sequence (used to represent symmetric keys)
    22  	RSA            KeyType = "RSA" // RSA
    23  )
    24  
    25  var allKeyTypes = map[KeyType]struct{}{
    26  	EC:       {},
    27  	OKP:      {},
    28  	OctetSeq: {},
    29  	RSA:      {},
    30  }
    31  
    32  var listKeyTypeOnce sync.Once
    33  var listKeyType []KeyType
    34  
    35  // KeyTypes returns a list of all available values for KeyType
    36  func KeyTypes() []KeyType {
    37  	listKeyTypeOnce.Do(func() {
    38  		listKeyType = make([]KeyType, 0, len(allKeyTypes))
    39  		for v := range allKeyTypes {
    40  			listKeyType = append(listKeyType, v)
    41  		}
    42  		sort.Slice(listKeyType, func(i, j int) bool {
    43  			return string(listKeyType[i]) < string(listKeyType[j])
    44  		})
    45  	})
    46  	return listKeyType
    47  }
    48  
    49  // Accept is used when conversion from values given by
    50  // outside sources (such as JSON payloads) is required
    51  func (v *KeyType) Accept(value interface{}) error {
    52  	var tmp KeyType
    53  	if x, ok := value.(KeyType); ok {
    54  		tmp = x
    55  	} else {
    56  		var s string
    57  		switch x := value.(type) {
    58  		case fmt.Stringer:
    59  			s = x.String()
    60  		case string:
    61  			s = x
    62  		default:
    63  			return errors.Errorf(`invalid type for jwa.KeyType: %T`, value)
    64  		}
    65  		tmp = KeyType(s)
    66  	}
    67  	if _, ok := allKeyTypes[tmp]; !ok {
    68  		return errors.Errorf(`invalid jwa.KeyType value`)
    69  	}
    70  
    71  	*v = tmp
    72  	return nil
    73  }
    74  
    75  // String returns the string representation of a KeyType
    76  func (v KeyType) String() string {
    77  	return string(v)
    78  }
    79  

View as plain text