Allow finishing a json path with single arrow ->

Now a json can be obtained without resorting to casting

- Previously: `/json_arr?select=data->>2::json`
- Now: `/json_arr?select=data->2`
This commit is contained in:
steve-chavez
2018-06-19 11:17:59 -05:00
committed by Steve Chávez
parent 30dfadec7b
commit d34afe861a
5 changed files with 61 additions and 19 deletions
+15 -8
View File
@@ -81,15 +81,22 @@ pFieldName = do
dash = isDash *> pure '-'
pJsonPath :: Parser JsonPath
pJsonPath = (<>) <$> many pJsonPathOp <*> ( (:[]) <$> (string "->>" *> (try pJIdx <|> pJKey)) )
pJsonPath = many pJsonOperation
where
pJsonPathOp :: Parser JsonPathOp
pJsonPathOp = try (string "->" *> pJIdx) <|> try (string "->" *> pJKey)
pJKey = JKey . toS <$> pFieldName
pJIdx = JIdx . toS <$> ((:) <$> option '+' (char '-') <*> many1 digit) <* pEnd
pEnd = try (void $ lookAhead (string "->")) <|>
try (void $ lookAhead (string "::")) <|>
try eof
pJsonOperation :: Parser JsonOperation
pJsonOperation = pJsonArrow <*> pJsonOperand
pJsonArrow =
try (string "->>" $> J2Arrow) <|>
try (string "->" $> JArrow)
pJsonOperand =
let pJKey = JKey . toS <$> pFieldName
pJIdx = JIdx . toS <$> ((:) <$> option '+' (char '-') <*> many1 digit) <* pEnd
pEnd = try (void $ lookAhead (string "->")) <|>
try (void $ lookAhead (string "::")) <|>
try eof in
try pJIdx <|> try pJKey
pField :: Parser Field
pField = lexeme $ (,) <$> pFieldName <*> option [] pJsonPath