Skip to content

Commit

Permalink
fix(katana_router_builder): Additional Comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Oct 16, 2022
1 parent 5664d68 commit 08933e1
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 116 deletions.
66 changes: 0 additions & 66 deletions packages/katana_router_builder/lib/common/extends_class.dart

This file was deleted.

9 changes: 7 additions & 2 deletions packages/katana_router_builder/lib/common/query_class.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
part of katana_router_builder;

/// Create classes for queries.
/// クエリー用のクラスを作成します。
///
/// Pass the value for the query to [model], the path created from the annotation to [path], and the annotation value to [annotation].
/// [model]にクエリー用の値を[path]にアノテーションから作成されたパスを[annotation]にアノテーションの値を渡します。
List<Class> queryClass(
ClassValue model,
PathValue path,
Expand All @@ -23,7 +28,7 @@ List<Class> queryClass(
..static = true
..modifier = FieldModifier.final$
..assignment = Code(
"RegExp(r\"^${path.path.trimQuery().trimString("/").replaceAllMapped(pathRegExp, (match) {
"RegExp(r\"^${path.path.trimQuery().trimString("/").replaceAllMapped(_pathRegExp, (match) {
return "(?<${match.group(1)!}>[^/?&]+)";
})}\$\")",
),
Expand Down Expand Up @@ -118,7 +123,7 @@ List<Class> queryClass(
..lambda = true
..returns = const Reference("String")
..body = Code(
"\"${path.path.trimQuery().trimString("/").replaceAllMapped(pathRegExp, (match) {
"\"${path.path.trimQuery().trimString("/").replaceAllMapped(_pathRegExp, (match) {
return "\$${match.group(1)?.toCamelCase()}";
})}\""),
),
Expand Down
5 changes: 5 additions & 0 deletions packages/katana_router_builder/lib/common/router_class.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
part of katana_router_builder;

/// Create a class for the router.
/// ルーター用のクラスを作成します。
///
/// Create a list of parsed queries in [queries].
/// [queries]に解析されたクエリーのリストを作成します。
List<Class> routerClass(
List<QueryValue> queries,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
part of katana_router_builder;

/// Automatic page generation.
/// ページの自動生成を行います。
class PageGenerator extends GeneratorForAnnotation<PagePath> {
/// Automatic page generation.
/// ページの自動生成を行います。
PageGenerator();

@override
Expand Down Expand Up @@ -75,7 +79,6 @@ class PageGenerator extends GeneratorForAnnotation<PagePath> {
..body.addAll(
[
...queryClass(_class, _path, _annotation),
// ...extendsClass(_class, _path, _annotation),
],
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
part of katana_router_builder;

/// Automatic generation of routers.
/// ルーターの自動生成を行います。
class RouterGenerator extends GeneratorForAnnotation<AppRoute> {
/// Automatic generation of routers.
/// ルーターの自動生成を行います。
RouterGenerator();

static const _typeChecker = TypeChecker.fromRuntime(PagePath);
Expand Down
5 changes: 2 additions & 3 deletions packages/katana_router_builder/lib/katana_router_builder.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022 mathru. All rights reserved.
// Copyright 2023 mathru. All rights reserved.

/// Building system for masamune packages. Automatic Model creation and Page route generation.
/// Building system for katana router packages. Automatic Page route generation.
///
/// To use, import `package:masamune_builder/masamune_builder.dart`.
///
Expand All @@ -24,7 +24,6 @@ import 'package:source_gen/source_gen.dart';

import 'package:source_gen/source_gen.dart' as source_gen;

part 'common/extends_class.dart';
part 'common/query_class.dart';
part 'common/router_class.dart';
part 'generator/page_generator.dart';
Expand Down
26 changes: 4 additions & 22 deletions packages/katana_router_builder/lib/src/config.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
part of katana_router_builder;

final pathRegExp = RegExp(r":([^/]+)");
final _pathRegExp = RegExp(r":([^/]+)");

final ignoreWords = <String>[
"uid",
"time",
"append",
"fetch",
"reload",
"save",
"loading",
"saving",
"create",
"delete",
"value",
"transaction",
"dispose",
"document",
"runtimeType",
"toString",
"addListener",
"removeListener",
"notifyListeners",
"hasListeners"
final _ignoreWords = <String>[
"build",
"query",
];
23 changes: 20 additions & 3 deletions packages/katana_router_builder/lib/value/annotation_value.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
part of katana_router_builder;

/// Class for storing annotation values.
/// アノテーションの値を保存するためのクラス。
///
/// Specify the class element in [element] and the annotation type in [annotationType].
/// [element]にクラスエレメント、[annotationType]にアノテーションのタイプを指定します。
class AnnotationValue {
AnnotationValue(this.element, this.type) {
final matcher = TypeChecker.fromRuntime(type);
/// Class for storing annotation values.
/// アノテーションの値を保存するためのクラス。
///
/// Specify the class element in [element] and the annotation type in [annotationType].
/// [element]にクラスエレメント、[annotationType]にアノテーションのタイプを指定します。
AnnotationValue(this.element, this.annotationType) {
final matcher = TypeChecker.fromRuntime(annotationType);

for (final meta in element.metadata) {
final obj = meta.computeConstantValue()!;
Expand All @@ -23,9 +33,16 @@ class AnnotationValue {

static final _regExp = RegExp(r"redirect:\s*\[([^\]]*)\]");

/// Class Element.
/// クラスエレメント。
final ClassElement element;
final Type type;

/// Annotation Type
/// アノテーションのタイプ
final Type annotationType;

/// List of `RedirectQuery`.
/// `RedirectQuery`の一覧。
late final List<String> redirectQueries;

@override
Expand Down
17 changes: 17 additions & 0 deletions packages/katana_router_builder/lib/value/class_value.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
part of katana_router_builder;

/// Defines the value of the class to which the annotation is assigned.
/// アノテーションが付与されたクラスの値を定義します。
///
/// Specify the element to live in [element].
/// [element]に暮らすエレメントを指定します。
class ClassValue {
/// Defines the value of the class to which the annotation is assigned.
/// アノテーションが付与されたクラスの値を定義します。
///
/// Specify the element to live in [element].
/// [element]に暮らすエレメントを指定します。
ClassValue(this.element) {
name = element.displayName;
final contstuctor = element.constructors.firstWhere((e) {
Expand All @@ -11,9 +21,16 @@ class ClassValue {
}).toList();
}

/// Class Element.
/// クラスエレメント。
final ClassElement element;

/// Class Name.
/// クラス名。
late final String name;

/// Class parameters.
/// クラスのパラメーター。
late final List<ParamaterValue> parameters;

@override
Expand Down
38 changes: 26 additions & 12 deletions packages/katana_router_builder/lib/value/parameter_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ part of katana_router_builder;

const _pageParamChecker = TypeChecker.fromRuntime(PageParam);

/// Parameter Value.
/// パラメーターの値。
///
/// Specify the parameter element in [element].
/// [element]にパラメーターエレメントを指定します。
class ParamaterValue {
/// Parameter Value.
/// パラメーターの値。
///
/// Specify the parameter element in [element].
/// [element]にパラメーターエレメントを指定します。
ParamaterValue(this.element) {
name = element.displayName;
if (ignoreWords.contains(name)) {
if (_ignoreWords.contains(name)) {
throw Exception(
"`$name` is a prohibited word. This word cannot be set as a parameter. Please specify another name.",
);
Expand All @@ -26,28 +36,32 @@ class ParamaterValue {
if (element.type.isNullable || element.isRequired) {
defaultValue = null;
} else {
// if (_defaultChecker.hasAnnotationOfExact(element)) {
// defaultValue = _defaultChecker
// .firstAnnotationOfExact(element)
// ?.getField("defaultValue")
// ?.toValue();
// if (defaultValue.runtimeType.toString() != type.toString()) {
// throw Exception(
// "Different types of DefaultValue:${defaultValue.runtimeType.toString()}!=${type.toString()} at $name($type)",
// );
// }
// } else
if (element.hasDefaultValue) {
defaultValue = element.defaultValueCode;
} else {
defaultValue = null;
}
}
}

/// Parameter Element.
/// パラメーターエレメント。
final ParameterElement element;

/// Default value of the parameter.
/// パラメーターのデフォルト値。
late final Object? defaultValue;

/// Parameter Type.
/// パラメーターのタイプ。
late final DartType type;

/// Name of parameter.
/// パラメーターの名前。
late final String name;

/// Name for the parameter path.
/// パラメーターのパス用の名前。
late final String pageParamName;

@override
Expand Down
17 changes: 16 additions & 1 deletion packages/katana_router_builder/lib/value/path_value.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
part of katana_router_builder;

/// Value for path.
/// パス用の値。
///
/// Specify the path given in the annotation in [path].
/// [path]にアノテーションに与えられたパスを指定します。
class PathValue {
/// Value for path.
/// パス用の値。
///
/// Specify the path given in the annotation in [path].
/// [path]にアノテーションに与えられたパスを指定します。
PathValue(this.path) {
parameters = pathRegExp.allMatches(path).mapAndRemoveEmpty(
parameters = _pathRegExp.allMatches(path).mapAndRemoveEmpty(
(e) => _PathValue(e.group(1)!),
);
}

/// Path value.
/// パスの値。
late final String path;

/// Parameters specified in the path.
/// パスに指定されたパラメーター。
late final List<_PathValue> parameters;

@override
Expand Down
15 changes: 15 additions & 0 deletions packages/katana_router_builder/lib/value/query_value.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
part of katana_router_builder;

/// Parameters for query.
/// クエリー用のパラメーター。
class QueryValue {
/// Parameters for query.
/// クエリー用のパラメーター。
const QueryValue({
required this.library,
required this.path,
required this.query,
required this.element,
});

/// Path for queries.
/// クエリー用のパス。
final String path;

/// Library for queries.
/// クエリー用のライブラリー。
final String library;

/// Query path for queries.
/// クエリー用のクエリーパス。
final String query;

/// Class elements for queries.
/// クエリー用のクラスエレメント。
final ClassElement element;
}

0 comments on commit 08933e1

Please sign in to comment.