-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathoptional.dart
More file actions
128 lines (111 loc) · 3.66 KB
/
optional.dart
File metadata and controls
128 lines (111 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'package:intl/intl.dart';
import 'common/common_library.dart';
/// Keeps track of whether the value has been set or not
enum OptionalState { unset, set }
/// Optional Class that allows users to pass in null or undefined for properties on a class.
/// If the state value is set, then we make sure to include it in the request over the wire.
/// If it's unset, then the value is ignored when sending over the wire.
class Optional<T> {
/// Instantiates deserializer.
Optional(this.deserializer, this.serializer);
/// Instantiates deserializer and serializer.
Optional.optional(this.deserializer, this.serializer);
/// State of the value. Is unset by default.
OptionalState state = OptionalState.unset;
/// Serializer for value.
DynamicSerializer<T> serializer;
/// Deserializer for value.
DynamicDeserializer<T> deserializer;
/// Current value.
T? _value;
/// Sets the value for the variable, and the state to `Set`.
set value(T? val) {
_value = val;
state = OptionalState.set;
}
/// Gets the current value of the object.
T? get value {
return _value;
}
/// Converts json to the value.
void fromJson(dynamic json) {
if (json is List) {
value = json.map((e) => deserializer(e)) as T;
} else {
value = deserializer(json as String);
}
}
/// Converts the value to String.
dynamic toJson() {
if (_value != null) {
return serializer(_value as T);
} else if (state == OptionalState.set) {
return null;
}
return '';
}
}
dynamic nativeToJson<T>(T type) {
if (type is bool ||
type is int ||
type is double ||
type is num ||
type is String) {
return type;
} else if (type is DateTime) {
final DateFormat formatter = DateFormat('yyyy-MM-dd');
return formatter.format(type);
}
throw UnimplementedError('This type is unimplemented: ${type.runtimeType}');
}
T nativeFromJson<T>(dynamic input) {
if ((input is bool && T == bool) ||
(input is int && T == int) ||
(input is double && T == double) ||
(input is num && T == num)) {
return input;
} else if (input is String) {
if (T == DateTime) {
return DateTime.parse(input) as T;
} else if (T == String) {
return input as T;
} else if (T == int) {
// Int64 is transmitted as String.
final value = BigInt.parse(input);
if (value.isValidInt) {
return value.toInt() as T;
} else {
throw UnsupportedError('BigInt ($input) too large for Dart int');
}
}
} else if (input is num) {
if (input is double && T == int) {
return input.toInt() as T;
} else if (input is int && T == double) {
return input.toDouble() as T;
}
}
throw UnimplementedError('This type is unimplemented: $T');
}
DynamicDeserializer<List<T>> listDeserializer<T>(
DynamicDeserializer<T> deserializer,
) {
return (dynamic data) =>
(data as List).map((e) => deserializer(e)).toList();
}
DynamicSerializer<List<T>> listSerializer<T>(DynamicSerializer<T> serializer) {
return (dynamic data) => (data as List).map((e) => serializer(e as T)).toList();
}