diff --git a/langs/go/go.go b/langs/go/go.go index 2bd4abd..401cdc5 100644 --- a/langs/go/go.go +++ b/langs/go/go.go @@ -2,6 +2,8 @@ package golang import ( "fmt" + "regexp" + "strings" "github.com/dave/jennifer/jen" "github.com/iancoleman/strcase" @@ -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)) diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go index 50854ff..780588d 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go +++ b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go @@ -7,6 +7,7 @@ import ( "fmt" runtime "github.com/mickeyreiss/firemodel/runtime" latlng "google.golang.org/genproto/googleapis/type/latlng" + "regexp" "time" ) @@ -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[a-zA-Z0-9]+)/test_models/(?P[a-zA-Z0-9]+)$") diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go index 9818f39..435c60d 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go +++ b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go @@ -4,6 +4,7 @@ package firemodel import ( "fmt" + "regexp" "time" ) @@ -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[a-zA-Z0-9]+)$")