File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 string
6+
7+ // Set sets a new value
8+ func (s * QuotedString ) Set (val string ) error {
9+ * s = QuotedString (trimQuotes (val ))
10+ return nil
11+ }
12+
13+ // Type returns the type of the value
14+ func (s * QuotedString ) Type () string {
15+ return "string"
16+ }
17+
18+ func (s * QuotedString ) String () string {
19+ return string (* s )
20+ }
21+
22+ func trimQuotes (value string ) string {
23+ lastIndex := len (value ) - 1
24+ for _ , char := range []byte {'\'' , '"' } {
25+ if value [0 ] == char && value [lastIndex ] == char {
26+ return value [1 :lastIndex ]
27+ }
28+ }
29+ return value
30+ }
Original file line number Diff line number Diff line change 1+ package opts
2+
3+ import (
4+ "github.com/docker/docker/pkg/testutil/assert"
5+ "testing"
6+ )
7+
8+ func TestQuotedStringSetWithQuotes (t * testing.T ) {
9+ qs := QuotedString ("" )
10+ assert .NilError (t , qs .Set ("\" something\" " ))
11+ assert .Equal (t , qs .String (), "something" )
12+ }
13+
14+ func TestQuotedStringSetWithMismatchedQuotes (t * testing.T ) {
15+ qs := QuotedString ("" )
16+ assert .NilError (t , qs .Set ("\" something'" ))
17+ assert .Equal (t , qs .String (), "\" something'" )
18+ }
19+
20+ func TestQuotedStringSetWithNoQuotes (t * testing.T ) {
21+ qs := QuotedString ("" )
22+ assert .NilError (t , qs .Set ("something" ))
23+ assert .Equal (t , qs .String (), "something" )
24+ }
You can’t perform that action at this time.
0 commit comments