forked from viamrobotics/viam-flutter-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.dart
90 lines (77 loc) · 2.81 KB
/
utils.dart
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
import 'package:grpc/grpc.dart';
import 'package:logger/logger.dart';
import 'gen/google/protobuf/struct.pb.dart';
final _logger = Logger();
extension NullableStringUtils on String? {
bool get isNullOrEmpty {
return this?.isEmpty ?? true;
}
bool get isNotNullNorEmpty {
return this?.isNotEmpty ?? false;
}
}
extension ValueUtils on Value {
dynamic toPrimitive() {
if (hasBoolValue()) return boolValue;
if (hasListValue()) return listValue.values.map((e) => e.toPrimitive()).toList();
if (hasNullValue()) return null;
if (hasNumberValue()) return numberValue;
if (hasStringValue()) return stringValue;
if (hasStructValue()) return structValue.fields.map((key, value) => MapEntry(key, value.toPrimitive()));
_logger.e('Value does not contain valid type');
throw const GrpcError.invalidArgument('value does not contain valid type');
}
}
extension StructUtils on Struct {
Map<String, dynamic> toMap() {
return fields.map((key, value) => MapEntry(key, value.toPrimitive()));
}
}
extension ListValueUtils<T> on List<T> {
Value toValue() {
final values = map((e) {
if (e is bool) return Value()..boolValue = e;
if (e is List<dynamic>) return e.toValue();
if (e is Map<String, dynamic>) return e.toValue();
if (e is num) return Value()..numberValue = e.toDouble();
if (e is String) return Value()..stringValue = e;
if (e == null) return Value()..nullValue = NullValue.NULL_VALUE;
_logger.e('Error converting the List to a Value');
throw const GrpcError.invalidArgument('List contains unsupported type');
});
return Value()..listValue = (ListValue()..values.addAll(values));
}
}
extension MapStructUtils on Map<String, dynamic> {
Struct toStruct() {
final Map<String, Value> result = {};
for (final entry in entries) {
final value = entry.value;
if (value is num) {
result[entry.key] = Value()..numberValue = value.toDouble();
} else if (value is String) {
result[entry.key] = Value()..stringValue = value;
} else if (value is bool) {
result[entry.key] = Value()..boolValue = value;
} else if (value is List<dynamic>) {
result[entry.key] = value.toValue();
} else if (value is Map<String, dynamic>) {
result[entry.key] = value.toValue();
} else if (value == null) {
result[entry.key] = Value()..nullValue = NullValue.NULL_VALUE;
} else {
_logger.e('Error converting the Map to a Struct');
throw const GrpcError.invalidArgument('Unsupported type');
}
}
return Struct()..fields.addAll(result);
}
Value toValue() {
return Value()..structValue = toStruct();
}
}
String getVersionMetadata() {
const String sdkVersion = 'v0.0.18';
const String apiTag = 'v0.1.328';
return 'flutter;$sdkVersion;$apiTag';
}