Last active
December 29, 2019 20:57
-
-
Save JNaeemGitonga/9c3f7046fc02220aad9e548cffaab423 to your computer and use it in GitHub Desktop.
func handleResultSendDbResponse(whichType string) (events.APIGatewayProxyResponse, error) {...}
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
/* | |
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