Skip to content

Commit

Permalink
Merge pull request #336 from posaune0423/fix/clause-composition-condi…
Browse files Browse the repository at this point in the history
…tion

[ FIX ]  modify conditions to flatten member clauses
  • Loading branch information
ponderingdemocritus authored Nov 25, 2024
2 parents b47c945 + f7297e8 commit 3c442a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
78 changes: 45 additions & 33 deletions packages/sdk/src/__tests__/convertQueryToClause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest";

import { MockSchemaType, schema } from "../__example__/index";
import { convertQueryToClause } from "../convertQuerytoClause";
import { QueryType, SchemaType } from "../types";
import { QueryType } from "../types";

describe("convertQueryToClause", () => {
it("should convert a single model query with conditions", () => {
Expand All @@ -30,19 +30,26 @@ describe("convertQueryToClause", () => {
operator: "Or",
clauses: [
{
Member: {
model: "world-player",
member: "id",
operator: "Eq",
value: { String: "1" },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Alice" },
Composite: {
operator: "And",
clauses: [
{
Member: {
model: "world-player",
member: "id",
operator: "Eq",
value: { String: "1" },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Alice" },
},
},
],
},
},
{
Expand Down Expand Up @@ -134,39 +141,44 @@ describe("convertQueryToClause", () => {

const result = convertQueryToClause(query, schema);

console.log("result", result);

// Updated expectation to match the actual output
expect(result).toEqual({
Composite: {
operator: "Or",
clauses: [
{
Member: {
model: "world-player",
member: "score",
operator: "Gt",
value: { Primitive: { U32: 100 } },
},
},
{
Composite: {
operator: "Or",
operator: "And",
clauses: [
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Alice" },
member: "score",
operator: "Gt",
value: { Primitive: { U32: 100 } },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Bob" },
Composite: {
operator: "Or",
clauses: [
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Alice" },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Bob" },
},
},
],
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/convertQuerytoClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ function processModels<T extends SchemaType>(
if (clause) {
if (
"Composite" in clause &&
clause.Composite.operator === "And"
clause.Composite.operator === "Or"
) {
// If the composite operator is "And", flatten the clauses
// If the composite operator is "Or", flatten the clauses
clauses.push(...clause.Composite.clauses);
} else {
// Otherwise, keep the composite as is to preserve logical structure
Expand Down Expand Up @@ -255,6 +255,7 @@ function buildWhereClause(
return memberClauses[0];
} else if (memberClauses.length > 1) {
return {
// conditions in member clause should be treated as "And" Conditions by default
Composite: {
operator: "And",
clauses: memberClauses,
Expand Down Expand Up @@ -299,7 +300,6 @@ function convertToPrimitive(value: any): torii.MemberValue {
* @throws {Error} - If the operator is unsupported.
*/
function convertOperator(operator: string): torii.ComparisonOperator {
console.log(operator);
switch (operator) {
case "$eq":
return "Eq";
Expand Down

0 comments on commit 3c442a6

Please sign in to comment.