balena-io / pinejs

Generate rest APIs from natural language models
Apache License 2.0
63 stars 11 forks source link

Should be able to $orderby using filtered reverse navigation properties #824

Open thgreasi opened 1 month ago

thgreasi commented 1 month ago
$orderby=device_tag(tag_key=%27test_key%27)/value%20desc
$orderby=device_tag(tag_key='test_key')/value desc

Afaict this is the OData spec path that allows this See: https://docs.oasis-open.org/odata/odata/v4.01/os/abnf/odata-abnf-construction-rules.txt

orderby     = ( "$orderby" / "orderby" ) EQ orderbyItem *( COMMA orderbyItem )
orderbyItem = commonExpr [ RWS ( "asc" / "desc" ) ]

commonExpr = ( primitiveLiteral
             / arrayOrObject
             / rootExpr
             / firstMemberExpr // <----------
             / functionExpr
             / negateExpr 
             / methodCallExpr 
             / parenExpr 
             / castExpr 
             / isofExpr
             / notExpr
             ) 
             [ addExpr 
             / subExpr 
             / mulExpr 
             / divExpr
             / divbyExpr 
             / modExpr
             ]  
             [ eqExpr 
             / neExpr 
             / ltExpr  
             / leExpr  
             / gtExpr 
             / geExpr 
             / hasExpr 
             / inExpr 
             ]
             [ andExpr 
             / orExpr 
             ] 

firstMemberExpr = memberExpr // <----------
                / inscopeVariableExpr [ "/" memberExpr ]

memberExpr = directMemberExpr // <----------
           / ( optionallyQualifiedEntityTypeName / optionallyQualifiedComplexTypeName ) "/" directMemberExpr

directMemberExpr = propertyPathExpr // <----------
                 / boundFunctionExpr 
                 / annotationExpr

propertyPathExpr = ( entityColNavigationProperty [ collectionNavigationExpr ] // <----------
                   / entityNavigationProperty    [ singleNavigationExpr ] 
                   / complexColProperty          [ complexColPathExpr ]
                   / complexProperty             [ complexPathExpr ] 
                   / primitiveColProperty        [ collectionPathExpr ]
                   / primitiveProperty           [ primitivePathExpr ]
                   / streamProperty              [ primitivePathExpr ]

entityColNavigationProperty = odataIdentifier

collectionNavigationExpr = [ "/" optionallyQualifiedEntityTypeName ]
                           ( collectionPathExpr
                           / keyPredicate [ singleNavigationExpr ] // <-----
                           / filterExpr [ collectionNavigationExpr ]
                           )

// keyPredicate section:
keyPredicate     = ( simpleKey
                               / compoundKey // <-----
                              / keyPathSegments
                              )
compoundKey      = OPEN keyValuePair *( COMMA keyValuePair ) CLOSE
keyValuePair     = ( primitiveKeyProperty / keyPropertyAlias  ) EQ ( parameterAlias / keyPropertyValue )
primitiveKeyProperty    = odataIdentifier
EQ     = "="
keyPropertyValue = primitiveLiteral

// singleNavigationExpr section:
singleNavigationExpr = "/" memberExpr

memberExpr = directMemberExpr // <----------
           / ( optionallyQualifiedEntityTypeName / optionallyQualifiedComplexTypeName ) "/" directMemberExpr

directMemberExpr = propertyPathExpr // <----------
                 / boundFunctionExpr 
                 / annotationExpr

propertyPathExpr = ( entityColNavigationProperty [ collectionNavigationExpr ]
                   / entityNavigationProperty    [ singleNavigationExpr ] 
                   / complexColProperty          [ complexColPathExpr ]
                   / complexProperty             [ complexPathExpr ] 
                   / primitiveColProperty        [ collectionPathExpr ]
                   / primitiveProperty           [ primitivePathExpr ] // <----------
                   / streamProperty              [ primitivePathExpr ]

primitiveProperty       = primitiveKeyProperty / primitiveNonKeyProperty
primitiveKeyProperty    = odataIdentifier

See: https://balena.zulipchat.com/#narrow/stream/346007-balena-io.2FbalenaCloud/topic/Server.20side.20device.20list.20-.20cycle.204/near/477162968

See: https://github.com/balena-io/pinejs/issues/577