Skip to content

Instantly share code, notes, and snippets.

@JNaeemGitonga
Last active December 29, 2019 20:57
Show Gist options
  • Save JNaeemGitonga/9c3f7046fc02220aad9e548cffaab423 to your computer and use it in GitHub Desktop.
Save JNaeemGitonga/9c3f7046fc02220aad9e548cffaab423 to your computer and use it in GitHub Desktop.
func handleResultSendDbResponse(whichType string) (events.APIGatewayProxyResponse, error) {...}
/*
handleResultSendDbResponse is used to handle multiple db responses
from insert and update operations it can also be extended to handle
delete operations when we are ready to add that functionality to the
API
*/
func handleResultSendDbResponse(whichType string) (events.APIGatewayProxyResponse, error) {
/*
you have to use reflect to dynamically get a value from a struct as
I am doing below. I used a struct so that I could reuse some code
I went through this just so I could access a property using a variable that
stored the prop name. In JS I could have done Story[whichType]
*/
reflectValue := reflect.ValueOf(&newDbResult)
underlyingStruct := reflect.Indirect(reflectValue).FieldByName(whichType).Elem()
reflectVal := underlyingStruct.Interface()
str := fmt.Sprintf("%+v", reflectVal)
return events.APIGatewayProxyResponse{
Body: str,
StatusCode: 200,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment