Skip to content

Commit

Permalink
endpoints: Simplify generate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Sep 18, 2024
1 parent d5b2b08 commit 5e4c48f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 76 deletions.
79 changes: 30 additions & 49 deletions endpoints/endpoints_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 6 additions & 15 deletions endpoints/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Partition struct {
name string
dnsSuffix string
regionRegex *regexp.Regexp
regions map[string]Region
}

// ID returns the identifier of the partition.
Expand All @@ -39,20 +40,15 @@ func (p Partition) RegionRegex() *regexp.Regexp {

// Regions returns a map of Regions for the partition, indexed by their ID.
func (p Partition) Regions() map[string]Region {
partitionAndRegion, ok := partitionsAndRegions[p.id]
if !ok {
return nil
}

return maps.Clone(partitionAndRegion.regions)
return maps.Clone(p.regions)
}

// DefaultPartitions returns a list of the partitions.
func DefaultPartitions() []Partition {
ps := make([]Partition, len(partitionsAndRegions))
ps := make([]Partition, 0, len(partitions))

for _, v := range partitionsAndRegions {
ps = append(ps, v.partition)
for _, p := range partitions {
ps = append(ps, p)
}

return ps
Expand All @@ -61,12 +57,7 @@ func DefaultPartitions() []Partition {
// PartitionForRegion returns the first partition which includes the specific Region.
func PartitionForRegion(ps []Partition, regionID string) (Partition, bool) {
for _, p := range ps {
partitionAndRegion, ok := partitionsAndRegions[p.id]
if !ok {
continue
}

if _, ok := partitionAndRegion.regions[regionID]; ok || partitionAndRegion.partition.regionRegex.MatchString(regionID) {
if _, ok := p.regions[regionID]; ok || p.regionRegex.MatchString(regionID) {
return p, true
}
}
Expand Down
17 changes: 5 additions & 12 deletions internal/generate/endpoints/output.go.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,14 @@ const (
{{- end }}
)

type partitionAndRegions struct {
partition Partition
regions map[string]Region
}

var (
partitionsAndRegions = map[string]partitionAndRegions{
partitions = map[string]Partition{
{{- range .Partitions }}
{{ .ID | KebabToTitle}}PartitionID: {
partition: Partition{
id: {{ .ID | KebabToTitle}}PartitionID,
name: "{{ .Name }}",
dnsSuffix: "{{ .DNSSuffix }}",
regionRegex: regexp.MustCompile(`{{ .RegionRegex }}`),
},
id: {{ .ID | KebabToTitle}}PartitionID,
name: "{{ .Name }}",
dnsSuffix: "{{ .DNSSuffix }}",
regionRegex: regexp.MustCompile(`{{ .RegionRegex }}`),
regions: map[string]Region{
{{- range .Regions }}
{{ .ID | KebabToTitle}}RegionID: {
Expand Down

0 comments on commit 5e4c48f

Please sign in to comment.