Last active
January 24, 2020 00:30
-
-
Save jeremeamia/75c0a6ae43b2f0d29cd360deadee0852 to your computer and use it in GitHub Desktop.
Revisions
-
jeremeamia revised this gist
Jan 24, 2020 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,10 +35,9 @@ func resolvePath(uri string, params interface{}) string { func getPathParams(params interface{}) map[string]string { p := map[string]string{} v := reflect.ValueOf(params) for i := 0; i < v.NumField(); i++ { val := v.Field(i).String() tag := v.Type().Field(i).Tag.Get("path") if tag != "" { p[tag] = val } -
jeremeamia created this gist
Jan 23, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ package main import ( "fmt" "reflect" "strings" ) type MyUrlParams struct { UserId string `path:"user",json:"-"` OrgId string `path:"org"` Foo string } func main() { params := MyUrlParams{ UserId: "abc123", OrgId: "xyz789", Foo: "bar", } uri := "/api/v1/users/{user}/orgs/{org}" fmt.Println(resolvePath(uri, params)) } func resolvePath(uri string, params interface{}) string { for search, replace := range getPathParams(params) { uri = strings.Replace(uri, "{"+search+"}", replace, 1) } return uri } func getPathParams(params interface{}) map[string]string { p := map[string]string{} v := reflect.ValueOf(params) t := reflect.TypeOf(params) for i := 0; i < t.NumField(); i++ { val := v.Field(i).String() tag := t.Field(i).Tag.Get("path") if tag != "" { p[tag] = val } } return p }