Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

make examples categories dynamic #164

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions clients/examplesrepo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
}

Expand All @@ -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])
})
}

Expand All @@ -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",
Expand All @@ -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",
},
},
},
}

Expand Down Expand Up @@ -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"
}
]
}
}`
4 changes: 1 addition & 3 deletions clients/examplesrepo/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
20 changes: 17 additions & 3 deletions clients/examplesrepo/utils.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand Down
24 changes: 20 additions & 4 deletions clients/examplesrepo/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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,
}

Expand All @@ -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])
})
}
Loading