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
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})*$/' |
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
export GOPATH=$HOME/porjects/go | |
export PATH="$PATH:$HOME/projects/go/bin" |
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
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"` | |
} |
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
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) |
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
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 |
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
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 | |
} |
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 |
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
func handleError(err error) (events.APIGatewayProxyResponse, error) { | |
return events.APIGatewayProxyResponse{ | |
Body: err.Error(), | |
StatusCode: 502, | |
}, err | |
} |
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
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 { |
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
func defaultReturn() (events.APIGatewayProxyResponse, error) { | |
return events.APIGatewayProxyResponse{ | |
Body: "whatever you did, it worked!", | |
StatusCode: 200, | |
}, nil | |
} |
NewerOlder