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

feat(cat-voices): proposal template models #1363

Merged
merged 22 commits into from
Dec 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
093487c
draft for setup segment
LynxLynxx Dec 9, 2024
508cb91
fix: delete unused files
LynxLynxx Dec 9, 2024
3757c4a
feat: dtos for properties
LynxLynxx Dec 10, 2024
93f2329
feat:creating dtos for proposal schema
LynxLynxx Dec 16, 2024
5e0c5c7
feat: merge mve3
LynxLynxx Dec 16, 2024
8eb4a81
feat: creating toModels
LynxLynxx Dec 17, 2024
9d91618
feat: sort section/elements by xorder
LynxLynxx Dec 18, 2024
4b5487a
feat: adding missing toModels for definitions
LynxLynxx Dec 18, 2024
e5f765c
fix: delete unused property from element class
LynxLynxx Dec 18, 2024
f696fb4
feat: creating generic_proposal.json
LynxLynxx Dec 19, 2024
2ad36ca
test: adding unit tests
LynxLynxx Dec 19, 2024
15fd551
feat: range class for easier representation for max min values
LynxLynxx Dec 20, 2024
5ed4e8c
feat: adding proper from/to Json for proposal_builder class
LynxLynxx Dec 20, 2024
41f56b3
feat: change name of the files to make it more generic -document-
LynxLynxx Dec 20, 2024
b326558
feat: change name of the document property widget
LynxLynxx Dec 20, 2024
e5e89d2
feat: adding equatable for classes
LynxLynxx Dec 20, 2024
9968cf8
Merge branch 'mve3' into feat/proposal-template-models
dtscalac Dec 23, 2024
0106505
chore: update chain follower
dtscalac Dec 23, 2024
4d70b4a
feat: sortingBy order
LynxLynxx Dec 23, 2024
cfdd91d
Merge branch 'feat/proposal-template-models' of github.com:input-outp…
LynxLynxx Dec 23, 2024
5141c75
Merge branch 'mve3' into feat/proposal-template-models
dtscalac Dec 23, 2024
b0783c5
chore: revert merge conflicts
dtscalac Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: adding proper from/to Json for proposal_builder class
LynxLynxx committed Dec 20, 2024
commit 5ed4e8c8cb07e73fbc3e50191f744b5fc791aa73
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:catalyst_voices_models/src/proposal_schema/schema.dart';
import 'package:json_annotation/json_annotation.dart';

part 'proposal_builder.g.dart';

@JsonSerializable()
class ProposalBuilder {
final String schema;
final List<ProposalBuilderSegment> segments;
@@ -40,20 +37,8 @@ class ProposalBuilder {
.toList(),
);
}

factory ProposalBuilder.fromJson(Map<String, dynamic> json) =>
_$ProposalBuilderFromJson(json);

Map<String, dynamic> toJson() {
final sections = <String, dynamic>{}..addAll({r'$schema': schema});
for (final section in segments) {
sections.addAll(section.toJson());
}
return sections;
}
}

@JsonSerializable()
class ProposalBuilderSegment {
final String id;
final List<ProposalBuilderSection> sections;
@@ -62,19 +47,6 @@ class ProposalBuilderSegment {
required this.id,
required this.sections,
});

factory ProposalBuilderSegment.fromJson(Map<String, dynamic> json) =>
_$ProposalBuilderSegmentFromJson(json);

Map<String, dynamic> toJson() {
final sections = <String, dynamic>{};
for (final section in this.sections) {
sections.addAll(section.toJson());
}
return {
id: sections,
};
}
}

@JsonSerializable()
@@ -86,22 +58,8 @@ class ProposalBuilderSection {
required this.id,
required this.elements,
});

factory ProposalBuilderSection.fromJson(Map<String, dynamic> json) =>
_$ProposalBuilderSectionFromJson(json);

Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
for (final element in elements) {
map.addAll(element.toJson());
}
return {
id: map,
};
}
}

@JsonSerializable()
class ProposalBuilderElement {
final String id;
final dynamic value;
@@ -110,11 +68,4 @@ class ProposalBuilderElement {
required this.id,
required this.value,
});

factory ProposalBuilderElement.fromJson(Map<String, dynamic> json) =>
_$ProposalBuilderElementFromJson(json);

Map<String, dynamic> toJson() => {
id: value,
};
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export 'cat_gateway.swagger.dart' show CatGateway;
export 'vit.swagger.dart' show Vit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_shared/catalyst_voices_shared.dart';
import 'package:json_annotation/json_annotation.dart';

part 'proposal_builder_dto.g.dart';

@JsonSerializable()
class ProposalBuilderDto {
@JsonKey(name: r'$schema')
final String schema;
@JsonKey(fromJson: _fromJsonSegments, toJson: _toJsonSegments)
final List<ProposalBuilderSegmentDto> segments;
const ProposalBuilderDto({
required this.schema,
required this.segments,
});

factory ProposalBuilderDto.fromJson(Map<String, dynamic> json) {
json['segments'] = Map<String, dynamic>.from(json)..remove(r'$schema');
return _$ProposalBuilderDtoFromJson(json);
}

factory ProposalBuilderDto.fromModel(ProposalBuilder model) {
return ProposalBuilderDto(
schema: model.schema,
segments:
model.segments.map(ProposalBuilderSegmentDto.fromModel).toList(),
);
}

Map<String, dynamic> toJson() {
final segments = <String, dynamic>{}..addAll({r'$schema': schema});
for (final segment in this.segments) {
segments.addAll(segment.toJson());
}
return segments;
}

ProposalBuilder toModel() {
return ProposalBuilder(
schema: schema,
segments: segments.map((e) => e.toModel()).toList(),
);
}

static Map<String, dynamic> _toJsonSegments(
List<ProposalBuilderSegmentDto> segments,
) {
final map = <String, dynamic>{};
for (final segment in segments) {
map[segment.id] = segment.toJson();
}
return map;
}

static List<ProposalBuilderSegmentDto> _fromJsonSegments(
Map<String, dynamic> json,
) {
final listOfSegments = json.convertMapToListWithIds();
return listOfSegments.map(ProposalBuilderSegmentDto.fromJson).toList();
}
}

@JsonSerializable()
class ProposalBuilderSegmentDto {
final String id;
@JsonKey(fromJson: _fromJsonSections, toJson: _toJsonSections)
final List<ProposalBuilderSectionDto> sections;

const ProposalBuilderSegmentDto({
required this.id,
required this.sections,
});

factory ProposalBuilderSegmentDto.fromJson(Map<String, dynamic> json) {
json['sections'] = Map<String, dynamic>.from(json)..remove('id');
return _$ProposalBuilderSegmentDtoFromJson(json);
}

factory ProposalBuilderSegmentDto.fromModel(ProposalBuilderSegment model) {
return ProposalBuilderSegmentDto(
id: model.id,
sections:
model.sections.map(ProposalBuilderSectionDto.fromModel).toList(),
);
}

Map<String, dynamic> toJson() {
final sections = <String, dynamic>{};
for (final section in this.sections) {
sections.addAll(section.toJson());
}
return {
id: sections,
};
}

ProposalBuilderSegment toModel() {
return ProposalBuilderSegment(
id: id,
sections: sections.map((e) => e.toModel()).toList(),
);
}

static Map<String, dynamic> _toJsonSections(
List<ProposalBuilderSectionDto> sections,
) {
final map = <String, dynamic>{};
for (final section in sections) {
map[section.id] = section.toJson();
}
return map;
}

static List<ProposalBuilderSectionDto> _fromJsonSections(
Map<String, dynamic> json,
) {
final listOfSections = json.convertMapToListWithIds();
return listOfSections.map(ProposalBuilderSectionDto.fromJson).toList();
}
}

@JsonSerializable()
class ProposalBuilderSectionDto {
final String id;
@JsonKey(fromJson: _fromJsonElements, toJson: _toJsonElements)
final List<ProposalBuilderElementDto> elements;

ProposalBuilderSectionDto({
required this.id,
required this.elements,
});

factory ProposalBuilderSectionDto.fromJson(Map<String, dynamic> json) {
json['elements'] = Map<String, dynamic>.from(json)..remove('id');
return _$ProposalBuilderSectionDtoFromJson(json);
}

factory ProposalBuilderSectionDto.fromModel(ProposalBuilderSection model) {
return ProposalBuilderSectionDto(
id: model.id,
elements:
model.elements.map(ProposalBuilderElementDto.fromModel).toList(),
);
}

Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
for (final element in elements) {
map.addAll(element.toJson());
}
return {
id: map,
};
}

ProposalBuilderSection toModel() {
return ProposalBuilderSection(
id: id,
elements: elements.map((e) => e.toModel()).toList(),
);
}

static Map<String, dynamic> _toJsonElements(
List<ProposalBuilderElementDto> elements,
) {
final map = <String, dynamic>{};
for (final element in elements) {
map[element.id] = element.value;
}
return map;
}

static List<ProposalBuilderElementDto> _fromJsonElements(
Map<String, dynamic> json,
) {
final listOfElements = json.convertMapToListWithIdsAndValues();
return listOfElements.map(ProposalBuilderElementDto.fromJson).toList();
}
}

@JsonSerializable()
class ProposalBuilderElementDto {
final String id;
final dynamic value;

const ProposalBuilderElementDto({
required this.id,
required this.value,
});

factory ProposalBuilderElementDto.fromJson(Map<String, dynamic> json) {
return _$ProposalBuilderElementDtoFromJson(json);
}

factory ProposalBuilderElementDto.fromModel(ProposalBuilderElement model) {
return ProposalBuilderElementDto(
id: model.id,
value: model.value,
);
}

Map<String, dynamic> toJson() => {
id: value,
};

ProposalBuilderElement toModel() {
return ProposalBuilderElement(
id: id,
value: value,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:convert';

import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_repositories/src/dto/proposal_builder_dto.dart';
import 'package:catalyst_voices_repositories/src/dto/schema_dto.dart';
import 'package:test/test.dart';

import '../../helpers/read_json.dart';

void main() {
const schemaPath =
'test/assets/0ce8ab38-9258-4fbc-a62e-7faa6e58318f.schema.json';

late Map<String, dynamic> schemaJson;

setUpAll(() {
schemaJson = json.decode(readJson(schemaPath)) as Map<String, dynamic>;
});

test('Converts segments list into object for JSON', () {
final schemaDto = SchemaDto.fromJson(schemaJson);
final schema = schemaDto.toModel();

final proposalBuilder = ProposalBuilder.build(schema);
final proposalBuilderDto = ProposalBuilderDto.fromModel(proposalBuilder);
final proposalBuilderJson = proposalBuilderDto.toJson();

for (final segment in proposalBuilderDto.segments) {
expect(proposalBuilderJson[segment.id], isA<Map<String, dynamic>>());
}
});

test('Converts object from JSON into List of segments', () {
final schemaDto = SchemaDto.fromJson(schemaJson);
final schema = schemaDto.toModel();

final proposalBuilder = ProposalBuilder.build(schema);
final proposalBuilderDto = ProposalBuilderDto.fromModel(proposalBuilder);

final proposalBuilderJson = proposalBuilderDto.toJson();
final proposalBuilderDtoFromJson =
ProposalBuilderDto.fromJson(proposalBuilderJson);

expect(
proposalBuilderDtoFromJson.segments.length,
proposalBuilderDto.segments.length,
);
});
}
Original file line number Diff line number Diff line change
@@ -11,4 +11,18 @@ extension MapToListExt on Map<String, dynamic> {

return list;
}

List<Map<String, dynamic>> convertMapToListWithIdsAndValues() {
final list = <Map<String, dynamic>>[];

for (final entry in entries) {
if (entry.key == r'$schema') continue;
LynxLynxx marked this conversation as resolved.
Show resolved Hide resolved
final value = <String, dynamic>{};
value['id'] = entry.key;
value['value'] = entry.value;
list.add(value);
}

return list;
}
}

Unchanged files with check annotations Beta

LET local_gen_code_path = packages/internal/catalyst_voices_repositories/lib/generated/api/
RUN melos l10n
RUN melos build_runner

Check failure on line 52 in catalyst_voices/Earthfile

GitHub Actions / ci / check / ./catalyst_voices+check-static-analysis

Error

The command RUN melos build_runner did not complete successfully. Exit code 1

Check failure on line 52 in catalyst_voices/Earthfile

GitHub Actions / test_reporting / Generate test reports

Error

The command RUN melos build_runner did not complete successfully. Exit code 1
IF [ $save_locally = true ]
RUN find . \( -name "*.g.dart" -o -name "*.freezed.dart" -o -name "*.chopper.dart" -o -name "*.swagger.dart" -o -name "*.openapi.dart" -o -name "*.gen.dart" -o -name "catalyst_voices_localizations*.dart" -o -name "cat_gateway.*.swagger.*" \)