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

Add Scope features #643

Merged
merged 31 commits into from
Jan 24, 2021
Merged

Add Scope features #643

merged 31 commits into from
Jan 24, 2021

Conversation

orekyuu
Copy link
Contributor

@orekyuu orekyuu commented Jan 11, 2021

refs: #641

Before

Until now there has been a need to write code that is not DRY.

example:

static Consumer<WhereDeclaration> onlyAdult(PropertyMetamodel<Integer> ageProp) {
  return w -> { w.gt(ageProp, 18) };
}

Entityql entityql;
UserEntity_ e;

// select * from users where age> 18;
entityql.from(e).where(onlyAdult(e.age));

// select * from users where age> 18 and role = 'customer';
entityql.from(e).where(onlyAdult(e.age).andThen(w -> { w.eq(e.role, 'customer') }));

After

Extend metamodel annotations to prevent non-DRY code.

@Entity(metamodel = @Metamodel(scope = { UserScope.class }))
class UserEntity {
  Long id;
  int age;
  String role;
}

class UserScope {
  public Consumer<WhereDeclaration> onlyAdult(UserEntity_ e) {
    return w -> w.gt(e.age, 18);
  }

  public Consumer<WhereDeclaration> onlyCustomer(UserEntity_ e) {
    return w -> w.eq(e.role, 'customer');
  }
}

Entityql entityql;
UserEntity_ e;

// select * from users where age> 18 and role = 'customer';
entityql.from(e).where(e.onlyAdult().andThen(e.onlyCustomer()));

Scope Class specifications

  • Requires default constructor.
  • The method must meet the following conditions
    • Requires first method parameter is Metamodel.
    • Requires public.
    • Requires non static.
    • Requires return value that is not void.
    • Ignored if these are not met.


VariableElement firstParameter = m.getParameters().get(0);
// TODO: この時点ではmetamodelが作られていないので代入可能かのチェックができない。SimpleNameの比較をしているが名前空間がちがえばすり抜けるため他の良い方法があれば
return firstParameter.asType().toString().equals(metamodel.getSimpleName());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, type checking cannot be performed correctly because it is before the Metamodel is generated.
Do you have a good idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea.
But I think it's OK.

@nakamura-to
Copy link
Member

Thanks. I'll review your pull request this weekend.

By the way, your sample code in the "After" section looks wrong.
The first parameter must be a Metamodel, right?

class UserScope {
  public Consumer<WhereDeclaration> onlyAdult(UserEntity_ e) {
    return w -> w.gt(e.age, 18);
  }

  public Consumer<WhereDeclaration> onlyCustomer(UserEntity_ e) {
    return w -> { w.eq(e.role, "customer");
  }
}

@orekyuu
Copy link
Contributor Author

orekyuu commented Jan 12, 2021

By the way, your sample code in the "After" section looks wrong.

Exactly. I'll fix it.

@orekyuu
Copy link
Contributor Author

orekyuu commented Jan 19, 2021

@nakamura-to Please review.

@nakamura-to
Copy link
Member

Hi, I will review this weekend.

@nakamura-to nakamura-to merged commit 7b1b345 into domaframework:master Jan 24, 2021
@nakamura-to
Copy link
Member

@orekyuu Thanks for your changes!

@nakamura-to nakamura-to linked an issue Jan 24, 2021 that may be closed by this pull request
@nakamura-to nakamura-to changed the title Add named expression Add Scope features Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add named expression
2 participants