-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: validate region name (#176)
* refactor: validate region name * fix: test client --------- Co-authored-by: ducnv <ducnguyenvan@bizflycloud.vn>
- Loading branch information
1 parent
9fbe218
commit 76dd9ba
Showing
6 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,8 @@ | |
# End of https://www.gitignore.io/api/go | ||
|
||
.idea/ | ||
|
||
# Config VSCode IDE | ||
.vscode | ||
|
||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |