Skip to content

Instantly share code, notes, and snippets.

View JNaeemGitonga's full-sized avatar
🌍
Working from home

Naeem JNaeemGitonga

🌍
Working from home
View GitHub Profile
@JNaeemGitonga
JNaeemGitonga / regex.txt
Created February 21, 2021 01:24 — forked from nerdsrescueme/regex.txt
Common Regex
Perl and PHP Regular Expressions
PHP regexes are based on the PCRE (Perl-Compatible Regular Expressions), so any regexp that works for one should be compatible with the other or any other language that makes use of the PCRE format. Here are some commonly needed regular expressions for both PHP and Perl. Each regex will be in string format and will include delimiters.
All Major Credit Cards
This regular expression will validate all major credit cards: American Express (Amex), Discover, Mastercard, and Visa.
//All major credit cards regex
'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/'
@JNaeemGitonga
JNaeemGitonga / pt-13.go.sh
Created December 29, 2019 21:35
shell variables
export GOPATH=$HOME/porjects/go
export PATH="$PATH:$HOME/projects/go/bin"
@JNaeemGitonga
JNaeemGitonga / pt-12.main.go
Created December 29, 2019 20:17
Story struct
type Story struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Author string `json:"author,,omitempty" bson:"author,,omitempty"`
Title string `json:"title,,omitempty" bson:"title,,omitempty"`
Content string `json:"content,,omitempty" bson:"content,,omitempty"`
Likes int `json:"likes,,omitempty" bson:"likes,,omitempty"`
}
@JNaeemGitonga
JNaeemGitonga / pt-11.main.go
Last active December 29, 2019 20:59
func updateStory(update string) (events.APIGatewayProxyResponse, error) {...}
func updateStory(ctx context.Context, update string) (events.APIGatewayProxyResponse, error) {
var raw Story // <--- this line and the following two are important here because if we didn't do this
data := []byte(update) // we wouldn't be able to use the incoming data as is since it is of type `string`.
if err := json.Unmarshal(data, &raw); err != nil {
handleError(err)
}
filter := bson.D{{"_id", raw.ID}} // <-- this is how we access those objects
upDate := bson.D{{"$set", bson.D{{"likes", raw.Likes}}}}
result, err := collection.UpdateOne(ctx, filter, upDate)
@JNaeemGitonga
JNaeemGitonga / pt-10.main.go
Last active December 30, 2019 21:44
func postStory(story string) (events.APIGatewayProxyResponse, error) {...}
func postStory(ctx context.Context, story string) (events.APIGatewayProxyResponse, error) {
err := client.Ping(ctx, readpref.Primary())
if err != nil {
return handleError(err)
}
var raw Story // <--- this and the folowing two lines was a big gotcha
s := strings.NewReader(story) // ^---> because the type of response.Body is `string` and you need it
json.NewDecoder(s).Decode(&raw) // ^---> to be what's defined in your schema/json
@JNaeemGitonga
JNaeemGitonga / pt-9.main.go
Created December 29, 2019 17:20
func marshalJSONAndSend(res []Story) (events.APIGatewayProxyResponse, error) {..}
func marshalJSONAndSend(res []Story) (events.APIGatewayProxyResponse, error) {
response, err := json.Marshal(res)
if err != nil {
return handleError(err)
}
return events.APIGatewayProxyResponse{
Body: string(response),
StatusCode: 200,
}, nil
}
@JNaeemGitonga
JNaeemGitonga / pt-8.main.go
Last active December 29, 2019 20:57
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
@JNaeemGitonga
JNaeemGitonga / pt-7.main.go
Last active December 29, 2019 19:17
func handleError(err error) (events.APIGatewayProxyResponse, error) {...}
func handleError(err error) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
Body: err.Error(),
StatusCode: 502,
}, err
}
func getStories(ctx context.Context) (events.APIGatewayProxyResponse, error) {
err := client.Ping(ctx, readpref.Primary())
if err != nil {
return handleError(err)
}
cursor, err := collection.Find(ctx, bson.D{})
defer cursor.Close(ctx)
if err != nil {
@JNaeemGitonga
JNaeemGitonga / pt-5.main.go
Created December 29, 2019 17:14
func defaultReturn() (events.APIGatewayProxyResponse, error) {...}
func defaultReturn() (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
Body: "whatever you did, it worked!",
StatusCode: 200,
}, nil
}