Skip to content

Commit

Permalink
Merge branch 'main' into feat/support_moreTimezone
Browse files Browse the repository at this point in the history
  • Loading branch information
airslice authored Nov 29, 2024
2 parents c43dcf4 + 43cff21 commit bec60dd
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 55 deletions.
41 changes: 33 additions & 8 deletions server/e2e/gql_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func testData(e *httpexpect.Expect) {
deleteProject(e, id) // delete
}

func projects(t *testing.T, ctx context.Context, r *repo.Container, count int, wID idx.ID[accountdomain.Workspace], name string, alias string) {
func projects(t *testing.T, ctx context.Context, r *repo.Container, count int, wID idx.ID[accountdomain.Workspace], name string, alias string, coreSupport bool) {
for i := range make([]int, count) {
p := project.New().
ID(project.NewID()).
Expand All @@ -566,7 +566,25 @@ func projects(t *testing.T, ctx context.Context, r *repo.Container, count int, w
Alias(alias).
IsArchived(false).
Deleted(false).
CoreSupport(true).
CoreSupport(coreSupport).
MustBuild()
err := r.Project.Save(ctx, p)
assert.Nil(t, err)
}
}

func projectsOldData(t *testing.T, ctx context.Context, r *repo.Container, count int, wID idx.ID[accountdomain.Workspace], name string, alias string) {
for i := range make([]int, count) {
p := project.New().
ID(project.NewID()).
Name(fmt.Sprintf(name+" name%d", i+1)).
Description(fmt.Sprintf(name+" description%d", i+1)).
ImageURL(lo.Must(url.Parse("https://test.com"))).
Workspace(wID).
Alias(alias).
IsArchived(false).
// Deleted(false). not exist
// CoreSupport(true). not exist
MustBuild()
err := r.Project.Save(ctx, p)
assert.Nil(t, err)
Expand All @@ -583,8 +601,8 @@ func TestGetProjectPagination(t *testing.T) {
e, r, _ := StartServerAndRepos(t, c, true, baseSeeder)
ctx := context.Background()

projects(t, ctx, r, 20, wID, "[wID]project", "ALIAS1")
projects(t, ctx, r, 20, wId1, "[wId1]project", "ALIAS2")
projects(t, ctx, r, 20, wID, "[wID]project", "ALIAS1", true)
projects(t, ctx, r, 20, wId1, "[wId1]project", "ALIAS2", true)

// ===== First request
requestBody := GraphQLRequest{
Expand Down Expand Up @@ -661,10 +679,17 @@ func TestGetProjectPaginationKeyword(t *testing.T) {
e, r, _ := StartServerAndRepos(t, c, true, baseSeeder)
ctx := context.Background()

projects(t, ctx, r, 10, wID, "AAAAAAA", "ALIAS1")
projects(t, ctx, r, 10, wID, "BBBBBBB", "ALIAS2")
projects(t, ctx, r, 10, wID, "AAAProjectAAAA", "ALIAS1")
projects(t, ctx, r, 10, wID, "BBBProjectBBBB", "ALIAS2")
// no match data
projects(t, ctx, r, 10, wID, "AAAAAAA", "ALIAS1", true)
projects(t, ctx, r, 10, wID, "BBBBBBB", "ALIAS2", true)

// match data
projects(t, ctx, r, 10, wID, "AAAProjectAAAA", "ALIAS3", true)
projects(t, ctx, r, 10, wID, "BBBProjectBBBB", "ALIAS4", true)

// no match data
projects(t, ctx, r, 10, wID, "AAAProjectAAAA", "ALIAS5", false)
projectsOldData(t, ctx, r, 10, wID, "BBBProjectBBBB", "ALIAS6")

// ===== First request
requestBody := GraphQLRequest{
Expand Down
34 changes: 8 additions & 26 deletions server/internal/infrastructure/mongo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,11 @@ func (r *Project) FindByWorkspace(ctx context.Context, id accountdomain.Workspac

filter := bson.M{
"team": id.String(),
"$and": []bson.M{
{
"$or": []bson.M{
{"deleted": false},
{"deleted": bson.M{"$exists": false}},
},
},
{
"$or": []bson.M{
{"coresupport": true},
{"coresupport": bson.M{"$exists": false}},
},
},
"$or": []bson.M{
{"deleted": false},
{"deleted": bson.M{"$exists": false}},
},
"coresupport": true,
}

if uFilter.Keyword != nil {
Expand All @@ -124,20 +115,11 @@ func (r *Project) FindStarredByWorkspace(ctx context.Context, id accountdomain.W
filter := bson.M{
"team": id.String(),
"starred": true,
"$and": []bson.M{
{
"$or": []bson.M{
{"deleted": false},
{"deleted": bson.M{"$exists": false}},
},
},
{
"$or": []bson.M{
{"coresupport": true},
{"coresupport": bson.M{"$exists": false}},
},
},
"$or": []bson.M{
{"deleted": false},
{"deleted": bson.M{"$exists": false}},
},
"coresupport": true,
}

return r.find(ctx, filter)
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"@lexical/utils": "0.12.0",
"@monaco-editor/react": "4.6.0",
"@popperjs/core": "2.11.8",
"@reearth/core": "0.0.7-alpha.20",
"@reearth/core": "0.0.7-alpha.21",
"@rot1024/use-transition": "1.0.0",
"@sentry/browser": "7.77.0",
"@seznam/compose-react-refs": "1.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("convertToLayerStyleValue", () => {

describe("parseStyleValue", () => {
it("should parse style value and return value as value type", () => {
const value = parseStyleValue("clamp");
const value = parseStyleValue("select", "clamp");

expect(value).toEqual({
valueType: "value",
Expand All @@ -190,7 +190,9 @@ describe("parseStyleValue", () => {
});
});
it("should parse style value and return expression as value type", () => {
const value = parseStyleValue({ expression: "color('#ffffff', 0.8)" });
const value = parseStyleValue("color", {
expression: "color('#ffffff', 0.8)"
});

expect(value).toEqual({
valueType: "expression",
Expand All @@ -203,7 +205,9 @@ describe("parseStyleValue", () => {

describe("parseConditions", () => {
it("should parse conditions correctly", () => {
const conditions = parseConditions([["${marker-size}==='medium'", "12"]]);
const conditions = parseConditions("number", [
["${marker-size}==='medium'", "12"]
]);

expect(conditions).toEqual([
{
Expand All @@ -216,6 +220,21 @@ describe("parseConditions", () => {
});
});

it("should parse conditions with URL values correctly", () => {
const conditions = parseConditions("model", [
["${type}==='1'", "'https://example.com/model.glb'"]
]);

expect(conditions).toEqual([
{
variable: "${type}",
operator: "===",
value: "'1'",
applyValue: "https://example.com/model.glb"
}
]);
});

describe("generateStyleValue", () => {
it("should generate style value'", () => {
expect(generateStyleValue(mockStyleNodes?.model[0])).toEqual(
Expand All @@ -225,7 +244,10 @@ describe("generateStyleValue", () => {
});

describe("generateConditions", () => {
const conditions = generateConditions(mockStyleNodes?.marker[0].conditions);
const conditions = generateConditions(
"number",
mockStyleNodes?.marker[0].conditions
);

it("should generate conditions'", () => {
expect(conditions).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
StyleValue,
ExpressionCondition,
Expression,
StyleValueType
StyleValueType,
AppearanceField
} from "./types";

export const convertToStyleNodes = (
Expand All @@ -24,6 +25,7 @@ export const convertToStyleNodes = (
.map(([k, v]: [string, unknown]) => {
const nodeRef = appearanceNodes[cur].find((n) => n.id === k);
const { valueType, value, expression, conditions } = parseStyleValue(
nodeRef?.field,
v as StyleValue
);
return {
Expand Down Expand Up @@ -100,16 +102,24 @@ export const checkExpressionAndConditions = (
return "value";
};

export const parseStyleValue = (v: StyleValue) => {
export const parseStyleValue = (
field: AppearanceField | undefined,
v: StyleValue
) => {
const valueType = checkExpressionAndConditions(v);
return {
valueType,
value: valueType === "value" ? v : undefined,
expression:
valueType === "expression" ? (v as Expression).expression : undefined,
valueType === "expression"
? unwrapExpression(field, (v as Expression).expression)
: undefined,
conditions:
valueType === "conditions"
? parseConditions((v as ExpressionCondition).expression.conditions)
? parseConditions(
field,
(v as ExpressionCondition).expression.conditions
)
: undefined
};
};
Expand Down Expand Up @@ -137,15 +147,20 @@ export const generateStyleValue = (node: StyleNode) => {
return node.value ?? "";
}
if (node.valueType === "expression") {
return { expression: node.expression ?? "" };
return { expression: wrapExpression(node.field, node.expression ?? "") };
}
if (node.valueType === "conditions") {
return { expression: { conditions: generateConditions(node.conditions) } };
return {
expression: {
conditions: generateConditions(node.field, node.conditions)
}
};
}
return undefined;
};

export const parseConditions = (
field: AppearanceField | undefined,
conditions: [string, string][]
): StyleCondition[] => {
const operatorRegex = new RegExp(
Expand All @@ -166,7 +181,7 @@ export const parseConditions = (
variable,
operator,
value,
applyValue: unwrapColor(applyValue)
applyValue: unwrapConditionAppliedValue(field, applyValue)
};
}
return null;
Expand All @@ -175,29 +190,113 @@ export const parseConditions = (
};

export const generateConditions = (
field: AppearanceField,
conditions?: StyleCondition[]
): [string, string][] => {
if (!conditions) return [];
return conditions.map((c) => {
return [
`${c.variable} ${c.operator} ${c.value}`,
wrapColor((c.applyValue ?? "").toString())
wrapConditionApplyValue((c.applyValue ?? "").toString(), field)
];
});
};

export const wrapConditionApplyValue = (
value: string,
field: AppearanceField
) => {
switch (field) {
case "color":
return wrapColor(value);
case "image":
case "text":
case "model":
return wrapString(value);
default:
return value;
}
};

export const unwrapConditionAppliedValue = (
field: AppearanceField | undefined,
value: string
) => {
switch (field) {
case "color":
return unwrapColor(value);
case "image":
case "text":
case "model":
return unwrapString(value);
default:
return value;
}
};

export const wrapExpression = (field: AppearanceField, expression: string) => {
if (/^\${.+}$/.test(expression)) {
return expression;
}
switch (field) {
case "color":
return wrapColor(expression);
case "image":
case "text":
case "model":
return wrapString(expression);
default:
return expression;
}
};

export const unwrapExpression = (
field: AppearanceField | undefined,
expression: string
) => {
if (/^\${.+}$/.test(expression)) {
return expression;
}
switch (field) {
case "color":
return unwrapColor(expression);
case "image":
case "text":
case "model":
return unwrapString(expression);
default:
return expression;
}
};

export const wrapColor = (maybeColor: string) => {
// check if color is a valid hex string
if (/^#[0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8}$/i.test(maybeColor)) {
if (
/^#(?:[0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i.test(maybeColor)
) {
return `color('${maybeColor}')`;
}
return maybeColor;
};

export const unwrapColor = (maybeWrappedColor: string) => {
// check if color is wrapped with color()
if (/^color\('.+'\)$/.test(maybeWrappedColor)) {
return maybeWrappedColor.slice(7, -2);
}
return maybeWrappedColor;
};

export const wrapString = (url: string) => {
if (!url) return url;
if (/^'.+'$/.test(url)) {
return url;
}
return `'${url}'`;
};

export const unwrapString = (maybeWrappedString: string) => {
if (!maybeWrappedString) return maybeWrappedString;
if (/^'.+'$/.test(maybeWrappedString)) {
return maybeWrappedString.slice(1, -1);
}
return maybeWrappedString;
};
Loading

0 comments on commit bec60dd

Please sign in to comment.