forked from enyo/stripe-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Experimental nested expanded fields idea #35
Draft
yurii-prykhodko-solid
wants to merge
5
commits into
main
Choose a base branch
from
expanded-experimental
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9027cf
Experimental nested expanded fields idea
yurii-prykhodko-solid 62c3d03
place json parsing inside the expanded field class
yurii-prykhodko-solid ee7c46c
subscription expansion
yurii-prykhodko-solid f80a82a
A full sample implementation of SubscriptionExpanded with configurabl…
yurii-prykhodko-solid 545756d
fmt
yurii-prykhodko-solid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:stripe/messages.dart'; | ||
|
||
class CustomerExpanded { | ||
final Customer customer; | ||
|
||
CustomerExpanded({ | ||
required this.customer, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:stripe/messages.dart'; | ||
|
||
class DiscountExpanded { | ||
final Discount discount; | ||
|
||
DiscountExpanded({ | ||
required this.discount, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,14 @@ | ||
import 'package:stripe/messages.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/payment_intent_expandable_field.dart'; | ||
import 'package:stripe/src/expanded/discount_expanded.dart'; | ||
|
||
class InvoiceExpanded { | ||
final Invoice invoice; | ||
final PaymentIntent? paymentIntent; | ||
final List<Discount>? discounts; | ||
final List<DiscountExpanded>? discounts; | ||
|
||
InvoiceExpanded({ | ||
required this.invoice, | ||
this.paymentIntent, | ||
this.discounts, | ||
}); | ||
|
||
factory InvoiceExpanded.fromJson( | ||
Map<String, dynamic> json, | ||
Set<InvoiceExpandableField> expand, | ||
) { | ||
PaymentIntent? paymentIntent; | ||
if (expand.contains(InvoiceExpandableField.paymentIntent)) { | ||
paymentIntent = PaymentIntentExpandableField().extract(json); | ||
} | ||
|
||
List<Discount>? discounts; | ||
if (expand.contains(InvoiceExpandableField.discounts)) { | ||
discounts = DiscountsExpandableField().extract(json); | ||
} | ||
|
||
return InvoiceExpanded( | ||
invoice: Invoice.fromJson(json), | ||
paymentIntent: paymentIntent, | ||
discounts: discounts, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,18 @@ | ||
import 'package:stripe/messages.dart'; | ||
import 'package:stripe/src/expanded.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart'; | ||
import 'package:stripe/src/expanded/customer_expanded.dart'; | ||
import 'package:stripe/src/expanded/discount_expanded.dart'; | ||
|
||
class SubscriptionExpanded { | ||
final Subscription subscription; | ||
final List<Discount>? discounts; | ||
final List<DiscountExpanded>? discounts; | ||
final InvoiceExpanded? latestInvoice; | ||
final Customer? customer; | ||
final CustomerExpanded? customer; | ||
|
||
SubscriptionExpanded({ | ||
required this.subscription, | ||
this.discounts, | ||
this.latestInvoice, | ||
this.customer, | ||
}); | ||
|
||
factory SubscriptionExpanded.fromJson( | ||
Map<String, dynamic> json, | ||
Set<SubscriptionExpandableField> expand, | ||
) { | ||
List<Discount>? discounts; | ||
if (expand.contains(SubscriptionExpandableField.discounts)) { | ||
discounts = const DiscountsExpandableField().extract(json); | ||
} | ||
|
||
InvoiceExpanded? latestInvoice; | ||
if (expand.contains(SubscriptionExpandableField.latestInvoice)) { | ||
latestInvoice = const LatestInvoiceExpandedExpandableField( | ||
expand: {InvoiceExpandableField.paymentIntent}, | ||
).extract(json); | ||
} | ||
|
||
Customer? customer; | ||
if (expand.contains(SubscriptionExpandableField.customer)) { | ||
customer = const CustomerExpandableField().extract(json); | ||
} | ||
|
||
return SubscriptionExpanded( | ||
subscription: Subscription.fromJson(json), | ||
discounts: discounts, | ||
latestInvoice: latestInvoice, | ||
customer: customer, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
lib/src/messages/enums/expandable_fields/invoice_expandable_field.dart
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
lib/src/messages/enums/expandable_fields/subscription_expandable_field.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,9 @@ import 'dart:async'; | |
|
||
import 'package:stripe/messages.dart'; | ||
import 'package:stripe/src/expanded.dart'; | ||
import 'package:stripe/src/utils/expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/customer_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/discounts_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expanded_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/latest_invoice_expandable_field.dart'; | ||
import 'package:stripe/src/utils/expandable_fields/subscription_expandable_field.dart'; | ||
|
||
import '../client.dart'; | ||
import '_resource.dart'; | ||
|
@@ -19,10 +18,10 @@ class SubscriptionResource extends Resource<Subscription> { | |
Future<SubscriptionExpanded> create(CreateSubscriptionRequest request) async { | ||
final response = await post(_resourceName, data: request.toJson()); | ||
|
||
return SubscriptionExpanded.fromJson(response, { | ||
SubscriptionExpandableField.discounts, | ||
SubscriptionExpandableField.latestInvoice, | ||
}); | ||
return SubscriptionExpandableField( | ||
discountsExpansion: DiscountsExpandableField(), | ||
latestInvoiceExpansion: LatestInvoiceExpandableField(), | ||
).extract(response)!; | ||
} | ||
|
||
Future<Subscription> retrieve(String id) async { | ||
|
@@ -32,40 +31,17 @@ class SubscriptionResource extends Resource<Subscription> { | |
|
||
Future<SubscriptionExpanded> retrieveExpanded( | ||
String id, { | ||
required Set<SubscriptionExpandableField> expand, | ||
required SubscriptionExpandableField expand, | ||
}) async { | ||
final expandableFields = _expandableFields(expand); | ||
final response = await get( | ||
'$_resourceName/$id', | ||
queryParameters: { | ||
'expand': expandableFields.map((e) => e.field).toList(), | ||
for (int i = 0; i < expand.innerNestedFields.length; i++) | ||
'expand[$i]': expand.innerNestedFields.elementAt(i), | ||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be extracted into a reusable piece. Maybe a utility on ExpandableField. |
||
}, | ||
); | ||
|
||
return SubscriptionExpanded.fromJson(response, expand); | ||
} | ||
|
||
Iterable<ExpandableField> _expandableFields( | ||
Set<SubscriptionExpandableField> fields, | ||
) { | ||
return fields.map( | ||
(field) => _expandableField(field), | ||
); | ||
} | ||
|
||
ExpandableField _expandableField( | ||
SubscriptionExpandableField field, | ||
) { | ||
switch (field) { | ||
case SubscriptionExpandableField.discounts: | ||
return DiscountsExpandableField(); | ||
case SubscriptionExpandableField.latestInvoice: | ||
return LatestInvoiceExpandedExpandableField( | ||
expand: {InvoiceExpandableField.paymentIntent}, | ||
); | ||
case SubscriptionExpandableField.customer: | ||
return CustomerExpandableField(); | ||
} | ||
return expand.parse(response); | ||
} | ||
|
||
Future<DataList<Subscription>> list( | ||
|
@@ -76,25 +52,20 @@ class SubscriptionResource extends Resource<Subscription> { | |
} | ||
|
||
Future<DataList<SubscriptionExpanded>> listExpanded({ | ||
required Set<SubscriptionExpandableField> expand, | ||
required SubscriptionExpandableField expand, | ||
ListSubscriptionsRequest? request, | ||
}) async { | ||
final expandableFields = _expandableFields(expand); | ||
|
||
final response = await get( | ||
_resourceName, | ||
queryParameters: { | ||
...?request?.toJson(), | ||
'expand': expandableFields.map((e) => 'data.${e.field}').toList(), | ||
'expand': expand.nestedFields.toList(), | ||
}, | ||
); | ||
|
||
return DataList<SubscriptionExpanded>.fromJson( | ||
response, | ||
(value) => SubscriptionExpanded.fromJson( | ||
value as Map<String, dynamic>, | ||
expand, | ||
), | ||
(value) => expand.extract(value as Map<String, Object?>)!, | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
lib/src/utils/expandable_fields/customer_expandable_field.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import 'package:stripe/messages.dart'; | ||
import 'package:stripe/src/expanded/customer_expanded.dart'; | ||
import 'package:stripe/src/utils/expandable_object_field.dart'; | ||
|
||
class CustomerExpandableField extends ExpandableObjectField<Customer> { | ||
class CustomerExpandableField extends ExpandableObjectField<CustomerExpanded> { | ||
@override | ||
String get field => 'customer'; | ||
|
||
const CustomerExpandableField(); | ||
|
||
@override | ||
Customer parse(Map<String, dynamic> object) => Customer.fromJson(object); | ||
CustomerExpanded parse(Map<String, dynamic> object) => | ||
CustomerExpanded(customer: Customer.fromJson(object)); | ||
|
||
@override | ||
String replacement(Customer parsedValue) => parsedValue.id; | ||
String replacement(CustomerExpanded parsedValue) => parsedValue.customer.id; | ||
} |
11 changes: 7 additions & 4 deletions
11
lib/src/utils/expandable_fields/discounts_expandable_field.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import 'package:stripe/messages.dart'; | ||
import 'package:stripe/src/expanded/discount_expanded.dart'; | ||
import 'package:stripe/src/utils/expandable_list_field.dart'; | ||
|
||
class DiscountsExpandableField extends ExpandableListField<Discount> { | ||
class DiscountsExpandableField extends ExpandableListField<DiscountExpanded> { | ||
@override | ||
String get field => 'discounts'; | ||
|
||
const DiscountsExpandableField(); | ||
|
||
@override | ||
String elementReplacement(Discount element) => element.id; | ||
String elementReplacement(DiscountExpanded element) => element.discount.id; | ||
|
||
@override | ||
Discount parseElement(Map<String, dynamic> element) => | ||
Discount.fromJson(element); | ||
DiscountExpanded parseElement(Map<String, dynamic> element) => | ||
DiscountExpanded( | ||
discount: Discount.fromJson(element), | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably the pattern we need to stick with. In most cases the included fields will have nesting of their own, and in order to minimize breaking changes as we add more "expanded" fields -- we'll need to make all nested fields
xExpanded
: