Skip to content

Commit

Permalink
sort metadata when building
Browse files Browse the repository at this point in the history
this saves complexity when reading/selecting
- sort the metadata entries by the name tokens before writing
- add an Idx field to the metadata struct
  • Loading branch information
tmck-code committed Apr 4, 2024
1 parent 294af98 commit c6c0f27
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/bin/pokedex/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"sort"
"strings"

"github.com/tmck-code/pokesay/src/bin"
Expand Down Expand Up @@ -98,6 +99,9 @@ func main() {
fmt.Println("- Found", len(cowfileFpaths), "cowfiles")
// Read pokemon names
pokemonNames := pokedex.ReadNames(args.FromMetadataFname)
nameTokens := pokedex.GatherMapKeys(pokemonNames)
sort.Strings(nameTokens)

fmt.Println("- Read", len(pokemonNames), "pokemon names from", args.FromMetadataFname)

fmt.Println("- Writing entries to file")
Expand All @@ -117,8 +121,9 @@ func main() {
uniqueNames := make(map[string][]int)
i := 0
pbar = bin.NewProgressBar(len(pokemonNames))
for key, name := range pokemonNames {
metadata := pokedex.CreateNameMetadata(i, key, name, args.FromDir, cowfileFpaths)
for i, key := range nameTokens {
name := pokemonNames[key]
metadata := pokedex.CreateNameMetadata(fmt.Sprintf("%04d", i), key, name, args.FromDir, cowfileFpaths)
pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(paths.MetadataDirPath, i))
pokemonMetadata = append(pokemonMetadata, *metadata)
uniqueNames[name.Slug] = append(uniqueNames[name.Slug], i)
Expand Down
4 changes: 3 additions & 1 deletion src/pokedex/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ type PokemonEntryMapping struct {
}

type PokemonMetadata struct {
Idx string
Name string
JapaneseName string
JapanesePhonetic string
Entries []PokemonEntryMapping
}

func NewMetadata(name string, japaneseName string, japanesePhonetic string, entryMap map[int][][]string) *PokemonMetadata {
func NewMetadata(idx string, name string, japaneseName string, japanesePhonetic string, entryMap map[int][][]string) *PokemonMetadata {

entries := make([]PokemonEntryMapping, 0)

Expand All @@ -30,6 +31,7 @@ func NewMetadata(name string, japaneseName string, japanesePhonetic string, entr
}

return &PokemonMetadata{
Idx: idx,
Name: name,
JapaneseName: japaneseName,
JapanesePhonetic: japanesePhonetic,
Expand Down
3 changes: 2 additions & 1 deletion src/pokedex/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Decompress(data []byte) []byte {
return resB.Bytes()
}

func CreateNameMetadata(idx int, key string, name PokemonName, rootDir string, fpaths []string) *PokemonMetadata {
func CreateNameMetadata(idx string, key string, name PokemonName, rootDir string, fpaths []string) *PokemonMetadata {
entryCategories := make(map[int][][]string, 0)

for i, fpath := range fpaths {
Expand All @@ -143,6 +143,7 @@ func CreateNameMetadata(idx int, key string, name PokemonName, rootDir string, f
}
}
return NewMetadata(
idx,
name.English,
name.Japanese,
name.JapanesePhonetic,
Expand Down

0 comments on commit c6c0f27

Please sign in to comment.