Skip to content

Commit

Permalink
to string in parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
juancastillo0 committed Jul 11, 2021
1 parent 266bcf4 commit aad4a38
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/gen_parsers/models/stores.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class ParserTokenNotifier {
isSerializable: true,
);
type.addVariant();

final _toStr = <String>[];
final _class = type.classes.first;
_class.typeConfig = type;
type.signatureNotifier.value = token.name.toClassName();
Expand All @@ -288,6 +290,12 @@ class ParserTokenNotifier {
for (final innerToken in list) {
if (innerToken.name.isEmpty) {
_props.add(null);
if (innerToken.value.isString) {
_toStr.add((innerToken.value as TokenValueString).value);
if (innerToken.trim) {
_toStr.add(' ');
}
}
continue;
}
final prop = _class.addProperty();
Expand All @@ -302,9 +310,32 @@ class ParserTokenNotifier {
? 'List<$_type>${_optional ? "?" : ""}'
: '$_type${_optional ? "?" : ""}';
_props.add(prop);

final _g = _optional
? '${prop.nameNotifier.value}!'
: prop.nameNotifier.value;
final v = innerToken.value;
final s = innerToken.repeat.canBeMany
? '\${$_g.join(" ")}'
: v is TokenValueSeparated &&
v.separator.value is TokenValueString
? "\${$_g.join('${(v.separator.value as TokenValueString).value}')}"
: '\${$_g}';
_toStr.add(_optional
? '\${${prop.nameNotifier.value} == null ? "" : "$s"}'
: s);
if (innerToken.trim) {
_toStr.add(' ');
}
}
context.add(type);
}
type.advancedConfig.customCodeNotifier.value = """
@override
String toString() {
return '${_toStr.map((e) => e == "'" ? "\\'" : e).join()}';
}
""";

_mapCode = list.length == 1
? ''
Expand Down Expand Up @@ -434,6 +465,12 @@ class ParserTokenNotifier {
: '.map((v) => ${type.signature}.${_class.name.asVariableName()}(value: v))',
);
}
type.advancedConfig.customCodeNotifier.value = '''
@override
String toString() {
return value.toString();
}
''';

context.add(type);
}
Expand Down
85 changes: 85 additions & 0 deletions lib/parsers/sql/generated_sqlite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Table {
final String name;
final List<Column> columns;

@override
String toString() {
return 'CREATE ${temp == null ? "" : "${temp!}"} TABLE ${name} ( ${columns.join(',')} ) ; ';
}

const Table({
required this.name,
required this.columns,
Expand Down Expand Up @@ -193,6 +198,11 @@ class Column {
final ColumnType? type;
final List<Constraint>? constraints;

@override
String toString() {
return '${name} ${type == null ? "" : "${type!}"} ${constraints == null ? "" : "${constraints!.join(" ")}"} ';
}

const Column({
required this.name,
this.type,
Expand Down Expand Up @@ -433,6 +443,11 @@ class Constraint {
final String name;
final ConstraintValue value;

@override
String toString() {
return 'CONSTRAINT ${name} ${value} ';
}

const Constraint({
required this.name,
required this.value,
Expand Down Expand Up @@ -486,6 +501,11 @@ class Constraint {
abstract class ConstraintValue {
const ConstraintValue._();

@override
String toString() {
return value.toString();
}

const factory ConstraintValue.primaryKey({
required PrimaryKey value,
}) = ConstraintPrimaryKey;
Expand Down Expand Up @@ -1090,6 +1110,11 @@ class PrimaryKey {
final PrimaryKeyOrder? order;
final ConflictClause clause;

@override
String toString() {
return 'PRIMARY KEY ${order == null ? "" : "${order!}"} ${clause} ';
}

const PrimaryKey({
required this.clause,
this.order,
Expand Down Expand Up @@ -1222,6 +1247,11 @@ class PrimaryKeyOrder {
class NotNull {
final ConflictClause clause;

@override
String toString() {
return 'NOT NULL ${clause} ';
}

const NotNull({
required this.clause,
});
Expand Down Expand Up @@ -1270,6 +1300,11 @@ class NotNull {
class Unique {
final ConflictClause clause;

@override
String toString() {
return 'UNIQUE ${clause} ';
}

const Unique({
required this.clause,
});
Expand Down Expand Up @@ -1318,6 +1353,11 @@ class Unique {
class DefaultValue {
final Expression value;

@override
String toString() {
return 'DEFAULT ${value} ';
}

const DefaultValue({
required this.value,
});
Expand Down Expand Up @@ -1393,6 +1433,11 @@ final conflictClause = (stringIgnoreCase('ON').trim() &
class ConflictClause {
final ConflictClauseValue value;

@override
String toString() {
return 'ON CONFLICT ${value} ';
}

const ConflictClause({
required this.value,
});
Expand Down Expand Up @@ -1569,6 +1614,11 @@ class ForeignKey {
final List<String> columnNames;
final List<ForeignKeyChangeClause>? changeClauses;

@override
String toString() {
return 'REFERENCES ${tableName} ( ${columnNames.join(',')} ) ${changeClauses == null ? "" : "${changeClauses!.join(" ")}"} ';
}

const ForeignKey({
required this.tableName,
required this.columnNames,
Expand Down Expand Up @@ -1677,6 +1727,11 @@ class ForeignKeyChangeClause {
final ForeignKeyChangeClauseType type;
final ForeignKeyChangeClauseValue value;

@override
String toString() {
return 'ON ${type} ${value} ';
}

const ForeignKeyChangeClause({
required this.type,
required this.value,
Expand Down Expand Up @@ -1808,6 +1863,11 @@ class ForeignKeyChangeClauseType {
abstract class ForeignKeyChangeClauseValue {
const ForeignKeyChangeClauseValue._();

@override
String toString() {
return value.toString();
}

const factory ForeignKeyChangeClauseValue.setNull({
required SetNull value,
}) = ForeignKeyChangeClauseSetNull;
Expand Down Expand Up @@ -2334,6 +2394,11 @@ class ForeignKeyChangeClauseNoAction extends ForeignKeyChangeClauseValue {
}

class SetNull {
@override
String toString() {
return 'SET NULL ';
}

const SetNull();

SetNull copyWith() {
Expand Down Expand Up @@ -2370,6 +2435,11 @@ class SetNull {
}

class SetDefault {
@override
String toString() {
return 'SET DEFAULT ';
}

const SetDefault();

SetDefault copyWith() {
Expand Down Expand Up @@ -2406,6 +2476,11 @@ class SetDefault {
}

class NoAction {
@override
String toString() {
return 'NO ACTION ';
}

const NoAction();

NoAction copyWith() {
Expand Down Expand Up @@ -2470,6 +2545,11 @@ final expression =
abstract class Expression {
const Expression._();

@override
String toString() {
return value.toString();
}

const factory Expression.number({
required double value,
}) = ExpressionNumber;
Expand Down Expand Up @@ -3237,6 +3317,11 @@ class ExpressionCurrentTimestamp extends Expression {
class Str {
final String value;

@override
String toString() {
return '\'${value}\'';
}

const Str({
required this.value,
});
Expand Down
2 changes: 2 additions & 0 deletions lib/types/templates/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ ${typeConfig.isDataValue && !typeConfig.isSumType ? "import 'dart:ui';" : ""}
class $signature {
${properties.map((p) => 'final ${p.type} ${p.name};').join('\n ')}
${!typeConfig.isSumType ? typeConfig.advancedConfig.customCode : ''}
${typeConfig.templates._const} $_classConstructor(${_templateClassParams()})${typeConfig.isSumType ? ": super._()" : ""};
${typeConfig.isSumType && typeConfig.sumTypeConfig.enumDiscriminant.value ? "@override\nType${typeConfig.name} get typeEnum => Type${typeConfig.name}.${name.asVariableName()};" : ""}
Expand Down

0 comments on commit aad4a38

Please sign in to comment.