github.com/kunnos/engine@v1.13.1/opts/quotedstring.go (about)

     1  package opts
     2  
     3  // QuotedString is a string that may have extra quotes around the value. The
     4  // quotes are stripped from the value.
     5  type QuotedString struct {
     6  	value *string
     7  }
     8  
     9  // Set sets a new value
    10  func (s *QuotedString) Set(val string) error {
    11  	*s.value = trimQuotes(val)
    12  	return nil
    13  }
    14  
    15  // Type returns the type of the value
    16  func (s *QuotedString) Type() string {
    17  	return "string"
    18  }
    19  
    20  func (s *QuotedString) String() string {
    21  	return string(*s.value)
    22  }
    23  
    24  func trimQuotes(value string) string {
    25  	lastIndex := len(value) - 1
    26  	for _, char := range []byte{'\'', '"'} {
    27  		if value[0] == char && value[lastIndex] == char {
    28  			return value[1:lastIndex]
    29  		}
    30  	}
    31  	return value
    32  }
    33  
    34  // NewQuotedString returns a new quoted string option
    35  func NewQuotedString(value *string) *QuotedString {
    36  	return &QuotedString{value: value}
    37  }