Skip to content

Commit

Permalink
Made regex able to parse both paths and firestore ref paths (#42)
Browse files Browse the repository at this point in the history
* Made regex able to parse both paths and firestore ref paths

* Update fixtures
  • Loading branch information
omzouai-visor authored Oct 26, 2018
1 parent 03906e6 commit ed90cb9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions langs/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,20 @@ func (m *GoModeler) writeModel(model *firemodel.SchemaModel, sourceCoder firemod
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), "$"))
start := "(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?"
g.Lit(fmt.Sprint(start, 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)
start := "(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?"
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, "$"))
g.Lit(fmt.Sprint(start, regex))
})

pathStructName := fmt.Sprint(model.Name, "PathStruct")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func TestModelPath(userId string, testModelId string) string {
}

// 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]+)$")
var TestModelRegexPath = regexp.MustCompile("(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?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]+)$")
var TestModelRegexNamedPath = regexp.MustCompile("(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?users/(?P<user_id>[a-zA-Z0-9]+)/test_models/(?P<test_model_id>[a-zA-Z0-9]+)")

// TestModelPathStruct is a struct that contains parts of a path of TestModel
type TestModelPathStruct struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestTimestampsPath(testTimestampsId string) string {
}

// TestTimestampsRegexPath is a regex that can be use to filter out firestore events of TestTimestamps
var TestTimestampsRegexPath = regexp.MustCompile("^timestamps/([a-zA-Z0-9]+)$")
var TestTimestampsRegexPath = regexp.MustCompile("(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?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]+)$")
var TestTimestampsRegexNamedPath = regexp.MustCompile("(?:projects/[^/]*/databases/[^/]*/documents/)?(?:/)?timestamps/(?P<test_timestamps_id>[a-zA-Z0-9]+)")

// TestTimestampsPathStruct is a struct that contains parts of a path of TestTimestamps
type TestTimestampsPathStruct struct {
Expand Down

0 comments on commit ed90cb9

Please sign in to comment.