diff --git a/clients/examplesrepo/client_test.go b/clients/examplesrepo/client_test.go index ed0270a..db243c4 100644 --- a/clients/examplesrepo/client_test.go +++ b/clients/examplesrepo/client_test.go @@ -54,8 +54,9 @@ func TestExamplesClient_GetAllExamples(t *testing.T) { require.NoError(t, err) examples, err := client.GetAllExamples() - assert.Len(t, examples, len(repositoryIndexModel.Examples.UDG)) - assert.Equal(t, repositoryIndexModel.Examples.UDG[0], examples[0]) + assert.Len(t, examples, repositoryIndexModelExamplesCount()) + assert.Equal(t, repositoryIndexModel.Examples["udg"][0], examples[2]) + assert.Equal(t, repositoryIndexModel.Examples["graphql"][0], examples[0]) }) } @@ -70,8 +71,9 @@ func TestExamplesClient_GetAllExamplesAsLocationIndexedMap(t *testing.T) { require.NoError(t, err) examplesMap, err := client.GetAllExamplesAsLocationIndexedMap() - assert.Len(t, examplesMap, len(repositoryIndexModel.Examples.UDG)) - assert.Equal(t, repositoryIndexModel.Examples.UDG[0], examplesMap[repositoryIndexModel.Examples.UDG[0].Location]) + assert.Len(t, examplesMap, repositoryIndexModelExamplesCount()) + assert.Equal(t, repositoryIndexModel.Examples["udg"][0], examplesMap[repositoryIndexModel.Examples["udg"][0].Location]) + assert.Equal(t, repositoryIndexModel.Examples["graphql"][0], examplesMap[repositoryIndexModel.Examples["graphql"][0].Location]) }) } @@ -89,9 +91,17 @@ func createRepositoryTestServer(t *testing.T, statusCode int) *httptest.Server { return httptest.NewServer(http.HandlerFunc(repositoryIndexHandler)) } +func repositoryIndexModelExamplesCount() int { + count := 0 + for _, examples := range repositoryIndexModel.Examples { + count += len(examples) + } + return count +} + var repositoryIndexModel = &RepositoryIndex{ Examples: ExamplesCategories{ - UDG: []ExampleMetadata{ + "udg": []ExampleMetadata{ { Location: "udg/first-demo", Name: "First UDG Demo", @@ -115,6 +125,17 @@ var repositoryIndexModel = &RepositoryIndex{ MinTykVersion: "5.0", }, }, + "graphql": []ExampleMetadata{ + { + Location: "graphql/star-wars", + Name: "Star Wars GQL API", + Description: "This GraphQL API retrieves all the Star Wars data you've ever wanted: Planets, Spaceships, Vehicles, People, Films and Species from all seven Star Wars films.", + Features: []string{ + "Authorization Token", + }, + MinTykVersion: "5.4", + }, + }, }, } @@ -143,6 +164,17 @@ const repositoryIndexJson = `{ ], "minTykVersion": "5.0" } - ] + ], + "graphql": [ + { + "location": "graphql/star-wars", + "name": "Star Wars GQL API", + "description": "This GraphQL API retrieves all the Star Wars data you've ever wanted: Planets, Spaceships, Vehicles, People, Films and Species from all seven Star Wars films.", + "features": [ + "Authorization Token" + ], + "minTykVersion": "5.4" + } + ] } }` diff --git a/clients/examplesrepo/models.go b/clients/examplesrepo/models.go index bee9263..06ecbe1 100644 --- a/clients/examplesrepo/models.go +++ b/clients/examplesrepo/models.go @@ -4,9 +4,7 @@ type RepositoryIndex struct { Examples ExamplesCategories `json:"examples"` } -type ExamplesCategories struct { - UDG []ExampleMetadata `json:"udg"` -} +type ExamplesCategories map[string][]ExampleMetadata type ExampleMetadata struct { Location string `json:"location"` diff --git a/clients/examplesrepo/utils.go b/clients/examplesrepo/utils.go index cf01c9a..779948b 100644 --- a/clients/examplesrepo/utils.go +++ b/clients/examplesrepo/utils.go @@ -1,11 +1,21 @@ package examplesrepo +import ( + "sort" +) + func IndexHasExamples(index *RepositoryIndex) bool { if index == nil { return false } - return len(index.Examples.UDG) > 0 + for category := range index.Examples { + if len(index.Examples[category]) > 0 { + return true + } + } + + return false } func MergeExamples(index *RepositoryIndex) []ExampleMetadata { @@ -14,10 +24,14 @@ func MergeExamples(index *RepositoryIndex) []ExampleMetadata { return examples } - for _, udgExample := range index.Examples.UDG { - examples = append(examples, udgExample) + for category := range index.Examples { + examples = append(examples, index.Examples[category]...) } + sort.Slice(examples, func(i, j int) bool { + return examples[i].Location < examples[j].Location + }) + return examples } diff --git a/clients/examplesrepo/utils_test.go b/clients/examplesrepo/utils_test.go index c8d1e35..3a41022 100644 --- a/clients/examplesrepo/utils_test.go +++ b/clients/examplesrepo/utils_test.go @@ -19,7 +19,7 @@ func TestIndexHasExamples(t *testing.T) { t.Run("should return true if index has at least one example", func(t *testing.T) { index := RepositoryIndex{ Examples: ExamplesCategories{ - UDG: []ExampleMetadata{ + "udg": []ExampleMetadata{ { Location: "location", }, @@ -42,15 +42,23 @@ func TestMergeExamples(t *testing.T) { Location: "udg", } + graphqlExample := ExampleMetadata{ + Location: "graphql", + } + index := RepositoryIndex{ Examples: ExamplesCategories{ - UDG: []ExampleMetadata{ + "udg": []ExampleMetadata{ udgExample, }, + "graphql": []ExampleMetadata{ + graphqlExample, + }, }, } expectedExamples := []ExampleMetadata{ + graphqlExample, udgExample, } @@ -74,18 +82,26 @@ func TestExamplesAsLocationIndexedMap(t *testing.T) { Location: "udg/example-2", } + graphqlExample1 := ExampleMetadata{ + Location: "graphql/example-1", + } + index := RepositoryIndex{ Examples: ExamplesCategories{ - UDG: []ExampleMetadata{ + "udg": []ExampleMetadata{ udgExample1, udgExample2, }, + "graphql": []ExampleMetadata{ + graphqlExample1, + }, }, } examplesMap := ExamplesAsLocationIndexedMap(&index) - assert.Len(t, examplesMap, 2) + assert.Len(t, examplesMap, 3) assert.Equal(t, udgExample1, examplesMap[udgExample1.Location]) assert.Equal(t, udgExample2, examplesMap[udgExample2.Location]) + assert.Equal(t, graphqlExample1, examplesMap[graphqlExample1.Location]) }) }