Skip to content

Commit 45f2b97

Browse files
committed
Generator: recast key to string for maps
1 parent bd5f691 commit 45f2b97

File tree

1 file changed

+12
-32
lines changed
  • internal/build/cmd/generate/commands/gentests

1 file changed

+12
-32
lines changed

internal/build/cmd/generate/commands/gentests/model.go

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ var reNumber = regexp.MustCompile(`^\d+$`)
3434
var reBaseFilename = regexp.MustCompile(`rest-api-spec/test/\w+/(.*$)`)
3535

3636
// TestPayload represents a single raw section (`---`) from the YAML file.
37-
//
3837
type TestPayload struct {
3938
Filepath string
4039
Payload interface{}
4140
}
4241

4342
// TestSuite represents a group of tests (a "section") in one file.
44-
//
4543
type TestSuite struct {
4644
Dir string
4745
Filepath string
@@ -56,7 +54,6 @@ type TestSuite struct {
5654
}
5755

5856
// Test represents a single named test.
59-
//
6057
type Test struct {
6158
Name string
6259
Filepath string
@@ -75,11 +72,9 @@ type Test struct {
7572
type Steps []Step
7673

7774
// Step represents an Action or an Assertion.
78-
//
7975
type Step interface{}
8076

8177
// Action represents an API action (`do`).
82-
//
8378
type Action struct {
8479
payload interface{}
8580

@@ -90,21 +85,18 @@ type Action struct {
9085
}
9186

9287
// Assertion represents a test assertion (`is_true`, `match`, etc).
93-
//
9488
type Assertion struct {
9589
payload interface{}
9690

9791
operation string
9892
}
9993

10094
// Stash represents the registry for `set` operations.
101-
//
10295
type Stash struct {
10396
payload interface{}
10497
}
10598

10699
// NewTestSuite returns a test group from the payloads.
107-
//
108100
func NewTestSuite(fpath string, payloads []TestPayload) TestSuite {
109101
ts := TestSuite{
110102
Dir: strings.Title(filepath.Base(filepath.Dir(fpath))),
@@ -227,7 +219,6 @@ func NewTestSuite(fpath string, payloads []TestPayload) TestSuite {
227219
}
228220

229221
// NewAction returns a new action from the payload.
230-
//
231222
func NewAction(payload interface{}) Action {
232223
defer func() {
233224
if r := recover(); r != nil {
@@ -260,19 +251,16 @@ func NewAction(payload interface{}) Action {
260251
}
261252

262253
// NewAssertion returns a new assertion from the payload.
263-
//
264254
func NewAssertion(operation string, payload interface{}) Assertion {
265255
return Assertion{operation: operation, payload: payload}
266256
}
267257

268258
// NewStash returns a new stash from the payload.
269-
//
270259
func NewStash(payload interface{}) Stash {
271260
return Stash{payload: payload}
272261
}
273262

274263
// Name returns a human name for the test suite.
275-
//
276264
func (ts TestSuite) Name() string {
277265
var b strings.Builder
278266
for _, v := range strings.Split(ts.Dir, ".") {
@@ -287,7 +275,6 @@ func (ts TestSuite) Name() string {
287275
}
288276

289277
// Filename returns a suitable filename for the test suite.
290-
//
291278
func (ts TestSuite) Filename() string {
292279
var b strings.Builder
293280

@@ -305,13 +292,11 @@ func (ts TestSuite) Filename() string {
305292
}
306293

307294
// SkipEsVersion returns true if the test suite should be skipped.
308-
//
309295
func (ts TestSuite) SkipEsVersion(minmax string) bool {
310296
return skipVersion(minmax)
311297
}
312298

313299
// BaseFilename extracts and returns the test filename in form of `foo/bar/10_qux.yml`.
314-
//
315300
func (t Test) BaseFilename() string {
316301
parts := reBaseFilename.FindStringSubmatch(t.Filepath)
317302
if len(parts) < 1 {
@@ -321,14 +306,12 @@ func (t Test) BaseFilename() string {
321306
}
322307

323308
// SkipEsVersion returns true if the test should be skipped.
324-
//
325309
func (t Test) SkipEsVersion(minmax string) bool {
326310
return skipVersion(minmax)
327311
}
328312

329313
// ContainsAssertion returns true when the set of steps
330314
// contain an assertion, or a specific assertion.
331-
//
332315
func (s Steps) ContainsAssertion(keys ...string) bool {
333316
for _, step := range s {
334317
if a, ok := step.(Assertion); ok {
@@ -357,7 +340,6 @@ func (s Steps) ContainsAssertion(keys ...string) bool {
357340
}
358341

359342
// ContainsCatch returns true when the set of steps contains the "catch" clause.
360-
//
361343
func (s Steps) ContainsCatch(keys ...string) bool {
362344
for _, step := range s {
363345
if a, ok := step.(Action); ok {
@@ -370,7 +352,6 @@ func (s Steps) ContainsCatch(keys ...string) bool {
370352
}
371353

372354
// ContainsStash returns true when the set of steps contains the "set" clause.
373-
//
374355
func (s Steps) ContainsStash(keys ...string) bool {
375356
for _, step := range s {
376357
if _, ok := step.(Stash); ok {
@@ -381,13 +362,11 @@ func (s Steps) ContainsStash(keys ...string) bool {
381362
}
382363

383364
// Method returns the API method name for the action.
384-
//
385365
func (a Action) Method() string {
386366
return utils.NameToGo(a.method)
387367
}
388368

389369
// Request returns the API request name for the action.
390-
//
391370
func (a Action) Request() string {
392371
var rParts []string
393372
parts := strings.Split(a.method, ".")
@@ -398,7 +377,6 @@ func (a Action) Request() string {
398377
}
399378

400379
// Params returns a map of parameters for the action.
401-
//
402380
func (a Action) Params() map[string]interface{} {
403381
var kk string
404382
out := make(map[string]interface{})
@@ -434,7 +412,6 @@ func (a Action) Params() map[string]interface{} {
434412
// Condition returns the condition for the assertion.
435413
//
436414
// TODO: Evaulate https://godoc.org/github.com/google/go-cmp/cmp
437-
//
438415
func (a Assertion) Condition() string {
439416
var (
440417
output string
@@ -714,7 +691,18 @@ default:
714691
// We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
715692
// a map[interface{}]interface{} that encoding/json fails to marshall
716693
// See https://play.golang.org/p/jhcXwg5dIrn
717-
expectedPayload := fmt.Sprintf("%#v", val)
694+
expectedPayload := func(val interface{}) string {
695+
expectedOutput := make(map[string]interface{})
696+
if cast, ok := val.(map[interface{}]interface{}); ok {
697+
for k, v := range cast {
698+
expectedOutput[fmt.Sprintf("%v", k)] = v
699+
}
700+
} else {
701+
expectedOutput = val.(map[string]interface{})
702+
}
703+
return fmt.Sprintf("%#v", expectedOutput)
704+
}(val)
705+
718706
expectedPayload = strings.ReplaceAll(expectedPayload, "map[interface {}]interface {}", "map[string]interface {}")
719707
output = ` actual = fmt.Sprintf("%v",` + escape(subject) + `)
720708
expected = fmt.Sprintf("%v",` + expectedPayload + `)
@@ -748,7 +736,6 @@ default:
748736
}
749737

750738
// Error returns an error handling for the failed assertion.
751-
//
752739
func (a Assertion) Error() string {
753740
var (
754741
output string
@@ -810,7 +797,6 @@ func (a Assertion) Error() string {
810797
}
811798

812799
// Key returns the stash key as a string.
813-
//
814800
func (s Stash) Key() string {
815801
vals := utils.MapValues(s.payload)
816802
if len(vals) < 1 {
@@ -821,7 +807,6 @@ func (s Stash) Key() string {
821807
}
822808

823809
// Value return the stash value as a string.
824-
//
825810
func (s Stash) FirstValue() string {
826811
vals := utils.MapKeys(s.payload)
827812
if len(vals) < 1 {
@@ -832,7 +817,6 @@ func (s Stash) FirstValue() string {
832817
}
833818

834819
// ExpandedValue returns the stash value in a container as a string.
835-
//
836820
func (s Stash) ExpandedValue() string {
837821
vals := utils.MapKeys(s.payload)
838822
if len(vals) < 1 {
@@ -845,7 +829,6 @@ func (s Stash) ExpandedValue() string {
845829
// expand "unwraps" a field name like `foo.bar.0.baz` into a proper Go structure
846830
//
847831
// See https://github.com/elastic/elasticsearch/tree/master/rest-api-spec/src/main/resources/rest-api-spec/test#dot-notation
848-
//
849832
func expand(s string, format ...string) string {
850833
var (
851834
b bytes.Buffer
@@ -906,7 +889,6 @@ func expand(s string, format ...string) string {
906889
}
907890

908891
// catchnil returns a condition which expands the input and checks if any part is not nil.
909-
//
910892
func catchnil(input string) string {
911893
var output string
912894

@@ -932,7 +914,6 @@ func catchnil(input string) string {
932914
}
933915

934916
// escape replaces unsafe characters in strings
935-
//
936917
func escape(s interface{}) string {
937918
if s, ok := s.(string); ok {
938919
// s = strings.Replace(s, `"`, `'`, -1)
@@ -945,7 +926,6 @@ func escape(s interface{}) string {
945926

946927
// skipVersion parses minmax string and returns
947928
// true when EsVersion is in the range.
948-
//
949929
func skipVersion(minmax string) bool {
950930
versions := strings.Split(minmax, "-")
951931
if len(versions) < 2 {

0 commit comments

Comments
 (0)