github.com/alimy/mir/v4@v4.1.0/internal/utils/set_method.go (about)

     1  // Copyright 2022 Michael Li <alimy@gility.net>. All rights reserved.
     2  // Use of this source code is governed by Apache License 2.0 that
     3  // can be found in the LICENSE file.
     4  
     5  package utils
     6  
     7  import (
     8  	"net/http"
     9  	"sort"
    10  )
    11  
    12  // HttpMethodSet http method set struct
    13  type HttpMethodSet map[string]struct{}
    14  
    15  func (s HttpMethodSet) Add(methods ...string) {
    16  	for _, method := range methods {
    17  		switch method {
    18  		case http.MethodGet,
    19  			http.MethodHead,
    20  			http.MethodPost,
    21  			http.MethodPut,
    22  			http.MethodPatch,
    23  			http.MethodDelete,
    24  			http.MethodConnect,
    25  			http.MethodOptions,
    26  			http.MethodTrace:
    27  			s[method] = struct{}{}
    28  		}
    29  	}
    30  }
    31  
    32  func (s HttpMethodSet) List() []string {
    33  	methods := make([]string, 0, len(s))
    34  	for m := range s {
    35  		methods = append(methods, m)
    36  	}
    37  	sort.Strings(methods)
    38  	return methods
    39  }