refactor(code_push_protocol): adjust protocol to support backend schema (#86)

This commit is contained in:
Felix Angelov
2023-03-17 10:21:10 -05:00
committed by GitHub
parent eb3c52360c
commit b1d4eef15e
18 changed files with 135 additions and 307 deletions
@@ -1,26 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'account.g.dart';
/// {@template account}
/// A user account which contains zero or more apps.
/// {@endtemplate}
@JsonSerializable()
class Account {
/// {@macro account}
Account({required this.apiKey, List<App>? apps}) : apps = apps ?? [];
/// Converts a Map<String, dynamic> to a [Account]
factory Account.fromJson(Map<String, dynamic> json) =>
_$AccountFromJson(json);
/// Converts a [Account] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$AccountToJson(this);
/// List of apps associated with this account.
final List<App> apps;
/// The api key associated with this account.
final String apiKey;
}
@@ -1,31 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
part of 'account.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Account _$AccountFromJson(Map<String, dynamic> json) => $checkedCreate(
'Account',
json,
($checkedConvert) {
final val = Account(
apiKey: $checkedConvert('api_key', (v) => v as String),
apps: $checkedConvert(
'apps',
(v) => (v as List<dynamic>?)
?.map((e) => App.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
fieldKeyMap: const {'apiKey': 'api_key'},
);
Map<String, dynamic> _$AccountToJson(Account instance) => <String, dynamic>{
'apps': instance.apps.map((e) => e.toJson()).toList(),
'api_key': instance.apiKey,
};
@@ -1,5 +1,4 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'app.g.dart';
@@ -9,8 +8,11 @@ part 'app.g.dart';
@JsonSerializable()
class App {
/// {@macro app}
App({required this.appId, List<Release>? releases})
: releases = releases ?? [];
const App({
required this.appId,
this.latestReleaseVersion,
this.latestPatchNumber,
});
/// Converts a Map<String, dynamic> to an [App]
factory App.fromJson(Map<String, dynamic> json) => _$AppFromJson(json);
@@ -21,6 +23,9 @@ class App {
/// The ID of the app.
final String appId;
/// List of releases associated with this app.
final List<Release> releases;
/// The latest release version of the app.
final String? latestReleaseVersion;
/// The latest patch number of the app.
final int? latestPatchNumber;
}
@@ -14,18 +14,22 @@ App _$AppFromJson(Map<String, dynamic> json) => $checkedCreate(
($checkedConvert) {
final val = App(
appId: $checkedConvert('app_id', (v) => v as String),
releases: $checkedConvert(
'releases',
(v) => (v as List<dynamic>?)
?.map((e) => Release.fromJson(e as Map<String, dynamic>))
.toList()),
latestReleaseVersion:
$checkedConvert('latest_release_version', (v) => v as String?),
latestPatchNumber:
$checkedConvert('latest_patch_number', (v) => v as int?),
);
return val;
},
fieldKeyMap: const {'appId': 'app_id'},
fieldKeyMap: const {
'appId': 'app_id',
'latestReleaseVersion': 'latest_release_version',
'latestPatchNumber': 'latest_patch_number'
},
);
Map<String, dynamic> _$AppToJson(App instance) => <String, dynamic>{
'app_id': instance.appId,
'releases': instance.releases.map((e) => e.toJson()).toList(),
'latest_release_version': instance.latestReleaseVersion,
'latest_patch_number': instance.latestPatchNumber,
};
@@ -1,6 +1,4 @@
export 'account.dart';
export 'app.dart';
export 'artifact.dart';
export 'patch.dart';
export 'registry.dart';
export 'release.dart';
export 'user.dart';
@@ -1,5 +1,4 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'patch.g.dart';
@@ -9,11 +8,7 @@ part 'patch.g.dart';
@JsonSerializable()
class Patch {
/// {@macro patch}
const Patch({
required this.number,
this.artifacts = const [],
this.channels = const [],
});
const Patch({required this.id, required this.number});
/// Converts a Map<String, dynamic> to a [Patch]
factory Patch.fromJson(Map<String, dynamic> json) => _$PatchFromJson(json);
@@ -21,12 +16,9 @@ class Patch {
/// Converts a [Patch] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$PatchToJson(this);
/// The unique patch identifier.
final int id;
/// The patch number (newer patches are larger).
final int number;
/// The list of channels associated with this patch.
final List<String> channels;
/// List of artifacts associated with this patch.
final List<Artifact> artifacts;
}
@@ -13,26 +13,14 @@ Patch _$PatchFromJson(Map<String, dynamic> json) => $checkedCreate(
json,
($checkedConvert) {
final val = Patch(
id: $checkedConvert('id', (v) => v as int),
number: $checkedConvert('number', (v) => v as int),
artifacts: $checkedConvert(
'artifacts',
(v) =>
(v as List<dynamic>?)
?.map((e) => Artifact.fromJson(e as Map<String, dynamic>))
.toList() ??
const []),
channels: $checkedConvert(
'channels',
(v) =>
(v as List<dynamic>?)?.map((e) => e as String).toList() ??
const []),
);
return val;
},
);
Map<String, dynamic> _$PatchToJson(Patch instance) => <String, dynamic>{
'id': instance.id,
'number': instance.number,
'channels': instance.channels,
'artifacts': instance.artifacts.map((e) => e.toJson()).toList(),
};
@@ -1,23 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'registry.g.dart';
/// {@template registry}
/// A collection of accounts and their respective apps
/// {@endtemplate}
@JsonSerializable()
class Registry {
/// {@macro registry}
const Registry({this.accounts = const []});
/// Converts a Map<String, dynamic> to a [Registry]
factory Registry.fromJson(Map<String, dynamic> json) =>
_$RegistryFromJson(json);
/// Converts a [Registry] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$RegistryToJson(this);
/// List of accounts associated with this registry.
final List<Account> accounts;
}
@@ -1,30 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
part of 'registry.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Registry _$RegistryFromJson(Map<String, dynamic> json) => $checkedCreate(
'Registry',
json,
($checkedConvert) {
final val = Registry(
accounts: $checkedConvert(
'accounts',
(v) =>
(v as List<dynamic>?)
?.map((e) => Account.fromJson(e as Map<String, dynamic>))
.toList() ??
const []),
);
return val;
},
);
Map<String, dynamic> _$RegistryToJson(Registry instance) => <String, dynamic>{
'accounts': instance.accounts.map((e) => e.toJson()).toList(),
};
@@ -1,27 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'release.g.dart';
/// {@template release}
/// A single release which contains zero or more patches.
/// {@endtemplate}
@JsonSerializable()
class Release {
/// {@macro release}
Release({required this.version, List<Patch>? patches})
: patches = patches ?? [];
/// Converts a Map<String, dynamic> to a [Release]
factory Release.fromJson(Map<String, dynamic> json) =>
_$ReleaseFromJson(json);
/// Converts a [Release] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$ReleaseToJson(this);
/// The version of the release.
final String version;
/// List of patches associated with this release.
final List<Patch> patches;
}
@@ -1,30 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
part of 'release.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Release _$ReleaseFromJson(Map<String, dynamic> json) => $checkedCreate(
'Release',
json,
($checkedConvert) {
final val = Release(
version: $checkedConvert('version', (v) => v as String),
patches: $checkedConvert(
'patches',
(v) => (v as List<dynamic>?)
?.map((e) => Patch.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$ReleaseToJson(Release instance) => <String, dynamic>{
'version': instance.version,
'patches': instance.patches.map((e) => e.toJson()).toList(),
};
@@ -0,0 +1,21 @@
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
/// {@template user}
/// A user account which contains zero or more apps.
/// {@endtemplate}
@JsonSerializable()
class User {
/// {@macro user}
const User({required this.id});
/// Converts a Map<String, dynamic> to a [User]
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
/// Converts a [User] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$UserToJson(this);
/// The unique user identifier.
final int id;
}
@@ -0,0 +1,24 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
part of 'user.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
User _$UserFromJson(Map<String, dynamic> json) => $checkedCreate(
'User',
json,
($checkedConvert) {
final val = User(
id: $checkedConvert('id', (v) => v as int),
);
return val;
},
);
Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
'id': instance.id,
};
@@ -0,0 +1,15 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group('App', () {
test('can be (de)serialized', () {
const app = App(
appId: 'my_app',
latestReleaseVersion: '1.0.0',
latestPatchNumber: 1,
);
expect(App.fromJson(app.toJson()).toJson(), equals(app.toJson()));
});
});
}
@@ -0,0 +1,19 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group('Artifact', () {
test('can be (de)serialized', () {
const artifact = Artifact(
arch: 'aarm64',
platform: 'android',
url: 'https://example.com',
hash: '#',
);
expect(
Artifact.fromJson(artifact.toJson()).toJson(),
equals(artifact.toJson()),
);
});
});
}
@@ -0,0 +1,14 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group('Patch', () {
test('can be (de)serialized', () {
const patch = Patch(id: 1, number: 2);
expect(
Patch.fromJson(patch.toJson()).toJson(),
equals(patch.toJson()),
);
});
});
}
@@ -1,99 +0,0 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group('Registry', () {
test('can be (de)serialized', () {
final registry = Registry(
accounts: [
Account(
apiKey: 'api_key1',
apps: [
App(
appId: 'app1',
releases: [
Release(
version: '1.0.0',
patches: [
const Patch(
number: 1,
artifacts: [
Artifact(
arch: 'aarm64',
platform: 'android',
url: 'localhost',
hash: '#',
)
],
channels: ['stable'],
),
const Patch(
number: 2,
artifacts: [
Artifact(
arch: 'aarm64',
platform: 'android',
url: 'localhost',
hash: '#',
)
],
channels: ['stable'],
)
],
),
Release(version: '1.0.1'),
],
),
App(appId: 'app2'),
],
),
Account(
apiKey: 'api_key2',
apps: [
App(
appId: 'app2',
releases: [
Release(
version: '1.0.0',
patches: [
const Patch(
number: 1,
artifacts: [
Artifact(
arch: 'aarm64',
platform: 'android',
url: 'localhost',
hash: '#',
)
],
channels: ['stable'],
),
const Patch(
number: 2,
artifacts: [
Artifact(
arch: 'aarm64',
platform: 'android',
url: 'localhost',
hash: '#',
)
],
channels: ['stable'],
)
],
),
Release(version: '2.0.0'),
],
),
],
),
],
);
expect(
Registry.fromJson(registry.toJson()).toJson(),
equals(registry.toJson()),
);
});
});
}
@@ -0,0 +1,14 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group('User', () {
test('can be (de)serialized', () {
const user = User(id: 1);
expect(
User.fromJson(user.toJson()).toJson(),
equals(user.toJson()),
);
});
});
}