Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to extend the conditional filter date type as shown in the above example. What interface or method do I need to implement? #10

Open
GuoxinL opened this issue Mar 17, 2020 · 5 comments

Comments

@GuoxinL
Copy link

GuoxinL commented Mar 17, 2020

json := `{
  "arr": [
    {
      "name": "jack",
      "create_time": "2020-01-01",
      "age": 10
    },
    {
      "name": "wow",
      "create_time": "2020-02-01",
      "age": 11
    }
  ]
}`
jsonslice.Get([]byte(json), "$.arr[?(@.create_time > format("yyyy-mm-dd", 2019-12-30))].name")

I want to extend the conditional filter date type as shown in the above example. What interface or method do I need to implement?

@bhmj
Copy link
Owner

bhmj commented Mar 20, 2020

I believe there are two questions implied here: what is the best way to specify a date (or date/time) constant and how to convert json date to comparable value (e.g. to give jsonslice a hint on how to parse a "create_time" field).

The first problem can be solved either the way you mentioned (using some kind of conversion function) or by introducing a strict format for datetime constants, like "YYYY-MM-DD HH:NN:SS TZ".
The second problem obviously require a conversion function since json date format may vary. Anyway, this problem is worth taking some time to think it over.

@GuoxinL
Copy link
Author

GuoxinL commented Mar 25, 2020

You're right, it's really worth thinking about, for data the user knows the format of the data for example for the "create time" field (yyyy-mm-dd) and since we've been thinking about the "create time" field for date types, we can directly describe the "create time" field, If the field type is known, the type of conditional filter parameter is also known, do you feel this is ok, the specific syntax above the function is a better way of grammar expression, a better way of grammar expression, not yet thought of.

@GuoxinL
Copy link
Author

GuoxinL commented Mar 25, 2020

My English is not very good, please forgive me if I don't express myself well

@GuoxinL
Copy link
Author

GuoxinL commented Apr 16, 2020

I would like to try to implement this first, please ask which interface I should implement

@bhmj
Copy link
Owner

bhmj commented Apr 16, 2020

In my opinion the optimal way would be a function call with two parameters:
date_parse(date_string, format_string). Format string should use at least these params:

Char Meaning Examples
Y Year 02018(YYYYY), 2018(YYYY), 18(YY)
M Month in year July(MMMM), Jul(MMM), 07(MM), 7(M)
D Day of the month 08(DD), 8(D)
H/h Hour 22(HH), 10(hh), 07(HH), 7(H), 7(h)
N/n miNute 03(NN/nn), 3(N/n)
S/s Second 06(SS/ss), 6(S/s)
F/f Millisecond 006(FFF/fff), 06(FF/ff), 6(F/f)
A/a AP/PM marker A.M., a.m. AM, am
X Timezone offset in hours +03; +0300; +03:00; GMT+3; GMT+0300; GMT+03:00
Any other character as is

The hour, minute and second char can be either lowercase or uppercase. The difference is only in hour char, H for 24h notation, h for 12h notation. Minutes and seconds can be described as 'N' or 'n' just to unify the whole time notation (HH:NN:SS or hh:nn:ss).

Format string Example Note
YYYY-MM-DD HH:NN:SS 2012-12-31 23:30:00 Typical UTC time, 24h notation
YYYY-MM-DD h:n a 2012-12-31 11:30 PM The same as above, 12h AM/PM notation
YYYY-MM-DD h:n a X 2012-12-31 11:30 PM GMT+03:00 The same as above, 12h notation, Europe/Moscow timezone
DD.MM.YYYY HH-NN-SS.FFF 31.12.2012 23-30-00.032 Different dividers, 24h, milliseconds
YYYY-M-DTHH:NN 2018-12-31T23:30 Non-space character separating date and time
YYYY-MM-DDTHH:NNZX 2018-12-31 23:30Z+0300 Timezone

So in jsonpath it must look like this:
jsonslice.Get([]byte(json), "$.arr[?(date_parse(@.create_time,'YYYY-MM-DD') > date_parse("2019-12-30", "YYYY-MM-DD"))].name")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants