github.com/cilium/cilium@v1.16.2/pkg/monitor/format/flags.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package format
     5  
     6  import (
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/spf13/pflag"
    11  )
    12  
    13  // Uint16Flags is a slice of unsigned 16-bit ints with some convenience methods.
    14  type Uint16Flags []uint16
    15  
    16  var _ pflag.Value = &Uint16Flags{}
    17  
    18  // String provides a human-readable string format of the received variable.
    19  func (i *Uint16Flags) String() string {
    20  	pieces := make([]string, 0, len(*i))
    21  	for _, v := range *i {
    22  		pieces = append(pieces, strconv.Itoa(int(v)))
    23  	}
    24  	return strings.Join(pieces, ", ")
    25  }
    26  
    27  // Set converts the specified value into an integer and appends it to the flags.
    28  // Returns an error if the value cannot be converted to a 16-bit unsigned value.
    29  func (i *Uint16Flags) Set(value string) error {
    30  	vUint64, err := strconv.ParseUint(value, 10, 16)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	*i = append(*i, uint16(vUint64))
    35  	return nil
    36  }
    37  
    38  // Type returns a human-readable string representing the type of the receiver.
    39  func (i *Uint16Flags) Type() string {
    40  	return "[]uint16"
    41  }
    42  
    43  // Has returns true of value exist
    44  func (i *Uint16Flags) Has(value uint16) bool {
    45  	for _, v := range *i {
    46  		if v == value {
    47  			return true
    48  		}
    49  	}
    50  
    51  	return false
    52  }