Skip to content

Commit

Permalink
refactor: validate region name
Browse files Browse the repository at this point in the history
  • Loading branch information
ducnv committed Feb 28, 2024
1 parent e583889 commit b5a785b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@
# End of https://www.gitignore.io/api/go

.idea/

# Config VSCode IDE
.vscode

build/
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"net/url"
"path"
"strings"

"github.com/bizflycloud/gobizfly/utils"
)

const (
Expand Down Expand Up @@ -117,7 +119,11 @@ func WithHTTPClient(client *http.Client) Option {
// WithRegionName sets the client region for Bizfly client.
func WithRegionName(region string) Option {
return func(c *Client) error {
c.regionName = region
regionName, err := utils.ParseRegionName(region)
if err != nil {
return err
}
c.regionName = regionName
return nil
}
}
Expand Down
15 changes: 15 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package constants

const (
HaNoiRegion = "HaNoi"
HoCHiMinhRegion = "HoChiMinh"
VcHaNoiRegion = "VC-HaNoi"
)

var RegionMapping = map[string]string{
"hn": HaNoiRegion,
"hanoi": HaNoiRegion,
"hcm": HoCHiMinhRegion,
"hochiminh": HoCHiMinhRegion,
"vc-hanoi": VcHaNoiRegion,
}
49 changes: 49 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gobizflyErr

import (
"bytes"
"html/template"
)

var tmpl = template.New("GobizflyTemplate")

type GobizflyErr struct {
Message string
Code string
Metadata map[string]interface{}
}

func (err GobizflyErr) Error() string {
message := err.GetMessage()
return message
}

func (err GobizflyErr) String() string {
message := err.GetMessage()
return message
}

func (err GobizflyErr) GetMessage() string {
newTmpl, tmplErr := tmpl.Parse(err.Message)
if tmplErr != nil {
return err.Message
}
var result bytes.Buffer
tmplErr = newTmpl.Execute(&result, err.Metadata)
if tmplErr != nil {
return err.Message
}
return result.String()
}

func (err GobizflyErr) SetMetadata(metadata map[string]interface{}) GobizflyErr {
err.Metadata = metadata
return err
}

var (
InvalidRegion = GobizflyErr{
Message: "Invalid region {{.Region}}",
Code: "InvalidRegion",
}
)
17 changes: 17 additions & 0 deletions utils/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import (
"strings"

"github.com/bizflycloud/gobizfly/constants"
gobizflyErr "github.com/bizflycloud/gobizfly/errors"
)

func ParseRegionName(region string) (string, error) {
lowerRegion := strings.ToLower(region)
regionName, ok := constants.RegionMapping[lowerRegion]
if !ok {
return "", gobizflyErr.InvalidRegion.SetMetadata(map[string]interface{}{"Region": region})
}
return regionName, nil
}

0 comments on commit b5a785b

Please sign in to comment.