forked from seaweedfs/seaweedfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support basic json filtering and selection
- Loading branch information
Showing
7 changed files
with
418 additions
and
186 deletions.
There are no files selected for viewing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,107 @@ | ||
package json | ||
|
||
func QueryJson(jsonLine string, query string) (jsonOutput string) { | ||
return jsonLine | ||
import ( | ||
"strconv" | ||
|
||
"github.com/chrislusf/seaweedfs/weed/query/sqltypes" | ||
"github.com/tidwall/gjson" | ||
"github.com/tidwall/match" | ||
) | ||
|
||
type Query struct { | ||
Field string | ||
Op string | ||
Value string | ||
} | ||
|
||
func QueryJson(jsonLine string, projections []string, query Query) (passedFilter bool, values []sqltypes.Value) { | ||
if filterJson(jsonLine, query) { | ||
passedFilter = true | ||
fields := gjson.GetMany(jsonLine, projections...) | ||
for _, f := range fields { | ||
values = append(values, sqltypes.MakeTrusted(sqltypes.Type(f.Type), sqltypes.StringToBytes(f.Raw))) | ||
} | ||
return | ||
} | ||
return false, nil | ||
} | ||
|
||
func filterJson(jsonLine string, query Query) bool{ | ||
|
||
value := gjson.Get(jsonLine, query.Field) | ||
|
||
// copied from gjson.go queryMatches() function | ||
rpv := query.Value | ||
|
||
if !value.Exists() { | ||
return false | ||
} | ||
if query.Op == "" { | ||
// the query is only looking for existence, such as: | ||
// friends.#(name) | ||
// which makes sure that the array "friends" has an element of | ||
// "name" that exists | ||
return true | ||
} | ||
switch value.Type { | ||
case gjson.String: | ||
switch query.Op { | ||
case "=": | ||
return value.Str == rpv | ||
case "!=": | ||
return value.Str != rpv | ||
case "<": | ||
return value.Str < rpv | ||
case "<=": | ||
return value.Str <= rpv | ||
case ">": | ||
return value.Str > rpv | ||
case ">=": | ||
return value.Str >= rpv | ||
case "%": | ||
return match.Match(value.Str, rpv) | ||
case "!%": | ||
return !match.Match(value.Str, rpv) | ||
} | ||
case gjson.Number: | ||
rpvn, _ := strconv.ParseFloat(rpv, 64) | ||
switch query.Op { | ||
case "=": | ||
return value.Num == rpvn | ||
case "!=": | ||
return value.Num != rpvn | ||
case "<": | ||
return value.Num < rpvn | ||
case "<=": | ||
return value.Num <= rpvn | ||
case ">": | ||
return value.Num > rpvn | ||
case ">=": | ||
return value.Num >= rpvn | ||
} | ||
case gjson.True: | ||
switch query.Op { | ||
case "=": | ||
return rpv == "true" | ||
case "!=": | ||
return rpv != "true" | ||
case ">": | ||
return rpv == "false" | ||
case ">=": | ||
return true | ||
} | ||
case gjson.False: | ||
switch query.Op { | ||
case "=": | ||
return rpv == "false" | ||
case "!=": | ||
return rpv != "false" | ||
case "<": | ||
return rpv == "true" | ||
case "<=": | ||
return true | ||
} | ||
} | ||
return false | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package json | ||
|
||
import "github.com/chrislusf/seaweedfs/weed/query/sqltypes" | ||
|
||
func ToJson(buf []byte, selections []string, values []sqltypes.Value) []byte { | ||
buf = append(buf, '{') | ||
for i, value := range values { | ||
if i > 0 { | ||
buf = append(buf, ',') | ||
} | ||
buf = append(buf, selections[i]...) | ||
buf = append(buf, ':') | ||
buf = append(buf, value.Raw()...) | ||
} | ||
buf = append(buf, '}') | ||
return buf | ||
} |
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