github.com/quay/claircore@v1.5.28/archop.go (about)

     1  package claircore
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql/driver"
     6  	"fmt"
     7  	"regexp"
     8  )
     9  
    10  type ArchOp uint
    11  
    12  const (
    13  	opInvalid ArchOp = iota // invalid
    14  
    15  	OpEquals       // equals
    16  	OpNotEquals    // not equals
    17  	OpPatternMatch // pattern match
    18  )
    19  
    20  func (o ArchOp) Cmp(a, b string) bool {
    21  	switch {
    22  	case b == "":
    23  		return true
    24  	case a == "":
    25  		return false
    26  	default:
    27  	}
    28  	switch o {
    29  	case OpEquals:
    30  		return a == b
    31  	case OpNotEquals:
    32  		return a != b
    33  	case OpPatternMatch:
    34  		re, err := regexp.Compile(b)
    35  		if err != nil {
    36  			return false
    37  		}
    38  		return re.MatchString(a)
    39  	default:
    40  	}
    41  	return false
    42  }
    43  
    44  func (o ArchOp) MarshalText() (text []byte, err error) {
    45  	return []byte(o.String()), nil
    46  }
    47  
    48  func (o *ArchOp) UnmarshalText(text []byte) error {
    49  	i := bytes.Index([]byte(_ArchOp_name), text)
    50  	if i == -1 {
    51  		*o = ArchOp(0)
    52  		return nil
    53  	}
    54  	idx := uint8(i)
    55  	for i, off := range _ArchOp_index {
    56  		if off == idx {
    57  			*o = ArchOp(i)
    58  			return nil
    59  		}
    60  	}
    61  	panic("unreachable")
    62  }
    63  
    64  func (o ArchOp) Value() (driver.Value, error) {
    65  	return o.String(), nil
    66  }
    67  
    68  func (o *ArchOp) Scan(i interface{}) error {
    69  	switch v := i.(type) {
    70  	case []byte:
    71  		return o.UnmarshalText(v)
    72  	case string:
    73  		return o.UnmarshalText([]byte(v))
    74  	case int64:
    75  		if v >= int64(len(_ArchOp_index)-1) {
    76  			return fmt.Errorf("unable to scan ArchOp from enum %d", v)
    77  		}
    78  		*o = ArchOp(v)
    79  	default:
    80  		return fmt.Errorf("unable to scan ArchOp from type %T", i)
    81  	}
    82  	return nil
    83  }