quotedstring.go 806 B

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