Skip to content

Commit

Permalink
check field names in the mongo example
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacov committed Dec 3, 2018
1 parent 14a523d commit 421b260
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/tsl_mongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/yaacov/tsl/pkg/tsl"
"github.com/yaacov/tsl/pkg/walkers/ident"
walker "github.com/yaacov/tsl/pkg/walkers/mongo"

"github.com/yaacov/tsl/cmd/model"
Expand All @@ -37,6 +38,26 @@ func check(err error) {
}
}

// columnNamesMap mapps between user namespace and the MongoDB document fields.
var columnNamesMap = map[string]string{
"title": "title",
"author": "author",
"spec.pages": "spec.pages",
"spec.rating": "spec.rating",
}

// checkColumnName checks if a coulumn name is valid in user space replace it
// with the mapped column name and returns and error if not a valid name.
func checkColumnName(s string) (string, error) {
// Chekc for column name in map.
if v, ok := columnNamesMap[s]; ok {
return v, nil
}

// If not found return string as is, and an error.
return s, fmt.Errorf("column \"%s\" not found", s)
}

func main() {
var err error
var client *mongo.Client
Expand Down Expand Up @@ -71,6 +92,10 @@ func main() {
tree, err := tsl.ParseTSL(*inputPtr)
check(err)

// Check and replace user identifiers with MongoDB document field names.
tree, err = ident.Walk(tree, checkColumnName)
check(err)

// Prepare a bson filter.
filter, err = walker.Walk(tree)
check(err)
Expand Down

0 comments on commit 421b260

Please sign in to comment.