github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/compression_gen.go (about)

     1  // Code generated by tools/cmd/genjwa/main.go. DO NOT EDIT.
     2  
     3  package jwa
     4  
     5  import (
     6  	"fmt"
     7  	"sort"
     8  	"sync"
     9  )
    10  
    11  // CompressionAlgorithm represents the compression algorithms as described in https://tools.ietf.org/html/rfc7518#section-7.3
    12  type CompressionAlgorithm string
    13  
    14  // Supported values for CompressionAlgorithm
    15  const (
    16  	Deflate    CompressionAlgorithm = "DEF" // DEFLATE (RFC 1951)
    17  	NoCompress CompressionAlgorithm = ""    // No compression
    18  )
    19  
    20  var muCompressionAlgorithms sync.RWMutex
    21  var allCompressionAlgorithms map[CompressionAlgorithm]struct{}
    22  var listCompressionAlgorithm []CompressionAlgorithm
    23  
    24  func init() {
    25  	muCompressionAlgorithms.Lock()
    26  	defer muCompressionAlgorithms.Unlock()
    27  	allCompressionAlgorithms = make(map[CompressionAlgorithm]struct{})
    28  	allCompressionAlgorithms[Deflate] = struct{}{}
    29  	allCompressionAlgorithms[NoCompress] = struct{}{}
    30  	rebuildCompressionAlgorithm()
    31  }
    32  
    33  // RegisterCompressionAlgorithm registers a new CompressionAlgorithm so that the jwx can properly handle the new value.
    34  // Duplicates will silently be ignored
    35  func RegisterCompressionAlgorithm(v CompressionAlgorithm) {
    36  	muCompressionAlgorithms.Lock()
    37  	defer muCompressionAlgorithms.Unlock()
    38  	if _, ok := allCompressionAlgorithms[v]; !ok {
    39  		allCompressionAlgorithms[v] = struct{}{}
    40  		rebuildCompressionAlgorithm()
    41  	}
    42  }
    43  
    44  // UnregisterCompressionAlgorithm unregisters a CompressionAlgorithm from its known database.
    45  // Non-existentn entries will silently be ignored
    46  func UnregisterCompressionAlgorithm(v CompressionAlgorithm) {
    47  	muCompressionAlgorithms.Lock()
    48  	defer muCompressionAlgorithms.Unlock()
    49  	if _, ok := allCompressionAlgorithms[v]; ok {
    50  		delete(allCompressionAlgorithms, v)
    51  		rebuildCompressionAlgorithm()
    52  	}
    53  }
    54  
    55  func rebuildCompressionAlgorithm() {
    56  	listCompressionAlgorithm = make([]CompressionAlgorithm, 0, len(allCompressionAlgorithms))
    57  	for v := range allCompressionAlgorithms {
    58  		listCompressionAlgorithm = append(listCompressionAlgorithm, v)
    59  	}
    60  	sort.Slice(listCompressionAlgorithm, func(i, j int) bool {
    61  		return string(listCompressionAlgorithm[i]) < string(listCompressionAlgorithm[j])
    62  	})
    63  }
    64  
    65  // CompressionAlgorithms returns a list of all available values for CompressionAlgorithm
    66  func CompressionAlgorithms() []CompressionAlgorithm {
    67  	muCompressionAlgorithms.RLock()
    68  	defer muCompressionAlgorithms.RUnlock()
    69  	return listCompressionAlgorithm
    70  }
    71  
    72  // Accept is used when conversion from values given by
    73  // outside sources (such as JSON payloads) is required
    74  func (v *CompressionAlgorithm) Accept(value interface{}) error {
    75  	var tmp CompressionAlgorithm
    76  	if x, ok := value.(CompressionAlgorithm); ok {
    77  		tmp = x
    78  	} else {
    79  		var s string
    80  		switch x := value.(type) {
    81  		case fmt.Stringer:
    82  			s = x.String()
    83  		case string:
    84  			s = x
    85  		default:
    86  			return fmt.Errorf(`invalid type for jwa.CompressionAlgorithm: %T`, value)
    87  		}
    88  		tmp = CompressionAlgorithm(s)
    89  	}
    90  	if _, ok := allCompressionAlgorithms[tmp]; !ok {
    91  		return fmt.Errorf(`invalid jwa.CompressionAlgorithm value`)
    92  	}
    93  
    94  	*v = tmp
    95  	return nil
    96  }
    97  
    98  // String returns the string representation of a CompressionAlgorithm
    99  func (v CompressionAlgorithm) String() string {
   100  	return string(v)
   101  }