-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
272 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1 @@ | ||
package aipcli | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestCompleteResourceName(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
toComplete string | ||
pattern string | ||
completion string | ||
ok bool | ||
}{ | ||
{ | ||
name: "mismatch", | ||
toComplete: "user", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "", | ||
ok: false, | ||
}, | ||
|
||
{ | ||
name: "empty input", | ||
toComplete: "", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "partial segment with singleton pattern", | ||
toComplete: "ship", | ||
pattern: "shippers", | ||
completion: "shippers", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "empty input with singleton pattern", | ||
toComplete: "", | ||
pattern: "shippers", | ||
completion: "shippers", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "empty input with variable pattern", | ||
toComplete: "", | ||
pattern: "{shipper}", | ||
completion: "", | ||
ok: false, | ||
}, | ||
|
||
{ | ||
name: "resource ID input with variable pattern", | ||
toComplete: "foo", | ||
pattern: "{shipper}", | ||
completion: "foo", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "partial segment", | ||
toComplete: "shi", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "full segment", | ||
toComplete: "shippers", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "full segment with slash", | ||
toComplete: "shippers/", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "partial resource ID", | ||
toComplete: "shippers/test", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/test", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "full resource ID", | ||
toComplete: "shippers/test/", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/test/shipments/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "partial second collection", | ||
toComplete: "shippers/foo/ship", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/foo/shipments/", | ||
ok: true, | ||
}, | ||
|
||
{ | ||
name: "second collection mismatch", | ||
toComplete: "shippers/foo/sit", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "", | ||
ok: false, | ||
}, | ||
|
||
{ | ||
name: "partial second resource ID", | ||
toComplete: "shippers/foo/shipments/bar", | ||
pattern: "shippers/{shipper}/shipments/{shipment}", | ||
completion: "shippers/foo/shipments/bar", | ||
ok: true, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, ok := completeResourceName(tt.toComplete, tt.pattern) | ||
assert.Equal(t, tt.ok, ok) | ||
assert.Equal(t, tt.completion, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package protoshell | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
// CompleteEnumValue returns valid values to complete for the provided enum values. | ||
func CompleteEnumValue(toComplete string, values protoreflect.EnumValueDescriptors) []string { | ||
result := make([]string, 0, values.Len()) | ||
for i := 0; i < values.Len(); i++ { | ||
value := string(values.Get(i).Name()) | ||
if strings.HasPrefix(value, toComplete) { | ||
result = append(result, value) | ||
} | ||
} | ||
if len(result) == 0 { | ||
return nil | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package protoshell | ||
|
||
import ( | ||
"testing" | ||
|
||
ltype "google.golang.org/genproto/googleapis/logging/type" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestCompleteEnum(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
toComplete string | ||
enum protoreflect.EnumValueDescriptors | ||
expected []string | ||
}{ | ||
{ | ||
name: "empty", | ||
toComplete: "", | ||
enum: ltype.LogSeverity_DEFAULT.Descriptor().Values(), | ||
expected: []string{ | ||
"DEFAULT", "DEBUG", "INFO", "NOTICE", "WARNING", "ERROR", "CRITICAL", "ALERT", "EMERGENCY", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "prefix match", | ||
toComplete: "E", | ||
enum: ltype.LogSeverity_DEFAULT.Descriptor().Values(), | ||
expected: []string{"ERROR", "EMERGENCY"}, | ||
}, | ||
|
||
{ | ||
name: "no match", | ||
toComplete: "Z", | ||
enum: ltype.LogSeverity_DEFAULT.Descriptor().Values(), | ||
expected: nil, | ||
}, | ||
|
||
{ | ||
name: "exact match", | ||
toComplete: "DEFAULT", | ||
enum: ltype.LogSeverity_DEFAULT.Descriptor().Values(), | ||
expected: []string{"DEFAULT"}, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := CompleteEnumValue(tt.toComplete, tt.enum) | ||
assert.DeepEqual(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package protoshell | ||
|
||
import ( | ||
"strings" | ||
|
||
"go.einride.tech/aip/resourcename" | ||
) | ||
|
||
// CompleteResourceName returns a value to complete for the provided resource name pattern. | ||
func CompleteResourceName(toComplete, pattern string) (string, bool) { | ||
toCompleteSegments := strings.Split(toComplete, "/") | ||
patternSegments := strings.Split(pattern, "/") | ||
if len(toCompleteSegments) > len(patternSegments) { | ||
return "", false | ||
} | ||
var result strings.Builder | ||
result.Grow(len(pattern)) | ||
for i, toCompleteSegment := range toCompleteSegments { | ||
patternSegment := patternSegments[i] | ||
if resourcename.Segment(patternSegment).IsVariable() { | ||
result.WriteString(toCompleteSegment) | ||
if i < len(toCompleteSegments)-1 { | ||
result.WriteByte('/') | ||
} | ||
continue | ||
} | ||
if toCompleteSegment == patternSegment { | ||
result.WriteString(patternSegment) | ||
if i < len(toCompleteSegments)-1 || i < len(patternSegments)-1 { | ||
result.WriteByte('/') | ||
} | ||
continue | ||
} | ||
if i < len(toCompleteSegments)-1 { | ||
return "", false | ||
} | ||
if !strings.HasPrefix(patternSegment, toCompleteSegment) { | ||
return "", false | ||
} | ||
result.WriteString(patternSegment) | ||
if i < len(patternSegments)-1 { | ||
result.WriteByte('/') | ||
} | ||
} | ||
return result.String(), result.String() != "" | ||
} |
Oops, something went wrong.