Skip to content

Commit

Permalink
Added Regex generators to listen for events (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
omzouai-visor authored and mickeyreiss committed Oct 19, 2018
1 parent 610ff11 commit edf6897
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions langs/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package golang

import (
"fmt"
"regexp"
"strings"

"github.com/dave/jennifer/jen"
"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -98,6 +100,24 @@ func (m *GoModeler) writeModel(model *firemodel.SchemaModel, sourceCoder firemod
}
})
}))
f.Commentf("%s is a regex that can be use to filter out firestore events of %s", fmt.Sprint(model.Name, "RegexPath"), model.Name)
f.Var().Id(fmt.Sprint(model.Name, "RegexPath")).Op("=").Qual("regexp", "MustCompile").CallFunc(func(g *jen.Group) {
regex := format
regex = regexp.QuoteMeta(regex)
g.Lit(fmt.Sprint("^", strings.Replace(format, "%s", "([a-zA-Z0-9]+)", -1), "$"))
})

f.Commentf("%s is a named regex that can be use to filter out firestore events of %s", fmt.Sprint(model.Name, "RegexNamedPath"), model.Name)
f.Var().Id(fmt.Sprint(model.Name, "RegexNamedPath")).Op("=").Qual("regexp", "MustCompile").CallFunc(func(g *jen.Group) {
regex := format
regex = regexp.QuoteMeta(regex)
for _, arg := range args {
repl := fmt.Sprint("(?P<", arg, ">[a-zA-Z0-9]+)")
regex = strings.Replace(regex, "%s", repl, 1)
}
g.Lit(fmt.Sprint("^", regex, "$"))
})

}

w, err := sourceCoder.NewFile(fmt.Sprint(strcase.ToSnake(model.Name), fileExtension))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
runtime "github.com/mickeyreiss/firemodel/runtime"
latlng "google.golang.org/genproto/googleapis/type/latlng"
"regexp"
"time"
)

Expand Down Expand Up @@ -63,3 +64,9 @@ type TestModel struct {
func TestModelPath(userId string, testModelId string) string {
return fmt.Sprintf("users/%s/test_models/%s", userId, testModelId)
}

// TestModelRegexPath is a regex that can be use to filter out firestore events of TestModel
var TestModelRegexPath = regexp.MustCompile("^users/([a-zA-Z0-9]+)/test_models/([a-zA-Z0-9]+)$")

// TestModelRegexNamedPath is a named regex that can be use to filter out firestore events of TestModel
var TestModelRegexNamedPath = regexp.MustCompile("^users/(?P<user_id>[a-zA-Z0-9]+)/test_models/(?P<test_model_id>[a-zA-Z0-9]+)$")
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package firemodel

import (
"fmt"
"regexp"
"time"
)

Expand All @@ -22,3 +23,9 @@ type TestTimestamps struct {
func TestTimestampsPath(testTimestampsId string) string {
return fmt.Sprintf("timestamps/%s", testTimestampsId)
}

// TestTimestampsRegexPath is a regex that can be use to filter out firestore events of TestTimestamps
var TestTimestampsRegexPath = regexp.MustCompile("^timestamps/([a-zA-Z0-9]+)$")

// TestTimestampsRegexNamedPath is a named regex that can be use to filter out firestore events of TestTimestamps
var TestTimestampsRegexNamedPath = regexp.MustCompile("^timestamps/(?P<test_timestamps_id>[a-zA-Z0-9]+)$")

0 comments on commit edf6897

Please sign in to comment.