Skip to content

Commit

Permalink
added dart - null safety
Browse files Browse the repository at this point in the history
Signed-off-by: Darwin Morocho <[email protected]>
  • Loading branch information
Darwin Morocho authored and lene committed Jun 10, 2021
1 parent 0d957df commit 71c34cb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 103 deletions.
7 changes: 3 additions & 4 deletions dart/lib/converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import 'package:tuple/tuple.dart';
///
class Converter {
final int precision;
int multiplier;
late int multiplier;
int lastValue = 0;

Converter(this.precision) {
multiplier = pow(10, precision);
multiplier = pow(10, precision) as int;
}

// Returns decoded int, new index in tuple
Expand Down Expand Up @@ -49,8 +49,7 @@ class Converter {
// Decode single coordinate (say lat|lng|z) starting at index
// Returns decoded coordinate, new index in tuple
Tuple2<double, int> decodeValue(List<String> encoded, int index) {
final Tuple2<int, int> result =
decodeUnsignedVarint(encoded, index);
final Tuple2<int, int> result = decodeUnsignedVarint(encoded, index);
double coordinate = 0;
int delta = result.item1;
if ((delta & 1) != 0) {
Expand Down
45 changes: 22 additions & 23 deletions dart/lib/flexible_polyline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class FlexiblePolyline {
/// @see FlexiblePolyline#getThirdDimension(String) getThirdDimension
/// @see LatLngZ
///
static List<LatLngZ> decode(String encoded) {
static List<LatLngZ> decode(String? encoded) {
if (encoded == null || encoded.trim().isEmpty) {
throw ArgumentError("Invalid argument!");
}
final List<LatLngZ> results = List<LatLngZ>();
final List<LatLngZ> results = [];
final _Decoder dec = _Decoder(encoded);
LatLngZ result;
LatLngZ? result;

do {
result = dec.decodeOne();
Expand All @@ -127,8 +127,8 @@ class FlexiblePolyline {
/// @param thirdDimPrecision Floating point precision for thirdDimension value
/// @return URL-safe encoded {@link String} for the given coordinates.
///
static String encode(List<LatLngZ> coordinates, int precision,
ThirdDimension thirdDimension, int thirdDimPrecision) {
static String encode(List<LatLngZ>? coordinates, int precision,
ThirdDimension? thirdDimension, int thirdDimPrecision) {
if (coordinates == null || coordinates.isEmpty) {
throw ArgumentError("Invalid coordinates!");
}
Expand Down Expand Up @@ -160,15 +160,15 @@ class FlexiblePolyline {
/// Single instance for decoding an input request.
class _Decoder {
final String encoded;
int index;
Converter latConverter;
Converter lngConverter;
Converter zConverter;
List<String> split;
late int index;
late Converter latConverter;
late Converter lngConverter;
late Converter zConverter;
late List<String> split;

int precision;
int thirdDimPrecision;
ThirdDimension thirdDimension;
late int precision;
late int thirdDimPrecision;
late ThirdDimension thirdDimension;

_Decoder(this.encoded) {
index = 0;
Expand All @@ -182,8 +182,7 @@ class _Decoder {
bool hasThirdDimension() => thirdDimension != ThirdDimension.ABSENT;

void _decodeHeader() {
final Tuple2<int, int> headerResult =
decodeHeaderFromString(split, index);
final Tuple2<int, int> headerResult = decodeHeaderFromString(split, index);
int header = headerResult.item1;
index = headerResult.item2;
precision = header & 15; // we pick the first 3 bits only
Expand All @@ -195,7 +194,8 @@ class _Decoder {
}

// Returns polyline header, new index in tuple.
static Tuple2<int, int> decodeHeaderFromString(List<String> encoded, int index) {
static Tuple2<int, int> decodeHeaderFromString(
List<String> encoded, int index) {
// Decode the header version
final Tuple2<int, int> result =
Converter.decodeUnsignedVarint(encoded, index);
Expand All @@ -207,7 +207,7 @@ class _Decoder {
return Converter.decodeUnsignedVarint(encoded, result.item2);
}

LatLngZ decodeOne() {
LatLngZ? decodeOne() {
if (index == encoded.length) {
return null;
}
Expand All @@ -218,8 +218,7 @@ class _Decoder {
lngConverter.decodeValue(split, index);
index = lngResult.item2;
if (hasThirdDimension()) {
final Tuple2<double, int> zResult =
zConverter.decodeValue(split, index);
final Tuple2<double, int> zResult = zConverter.decodeValue(split, index);
index = zResult.item2;
return LatLngZ(latResult.item1, lngResult.item1, zResult.item1);
}
Expand All @@ -233,9 +232,9 @@ class _Encoder {
final int precision;
final ThirdDimension thirdDimension;
final int thirdDimPrecision;
Converter latConverter;
Converter lngConverter;
Converter zConverter;
late Converter latConverter;
late Converter lngConverter;
late Converter zConverter;
String result = '';

_Encoder(this.precision, this.thirdDimension, this.thirdDimPrecision) {
Expand Down Expand Up @@ -280,7 +279,7 @@ class _Encoder {
}
}

void add(LatLngZ tuple) {
void add(LatLngZ? tuple) {
if (tuple == null) {
throw ArgumentError("Invalid LatLngZ tuple");
}
Expand Down
8 changes: 4 additions & 4 deletions dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ homepage: https://github.com/heremaps/flexible-polyline
repository: https://github.com/heremaps/flexible-polyline/dart

environment:
sdk: '>=2.2.2 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
path: ^1.7.0
tuple: ^1.0.3
path: ^1.8.0
tuple: ^2.0.0

dev_dependencies:
test: ^1.0.0
test: ^1.17.5
11 changes: 11 additions & 0 deletions dart/test/decode_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:test/test.dart';

import '../lib/flexible_polyline.dart';

void main() {
test('decode polyline - null safety', () {
final encoded = "BFoz5xJ67i1B1B7PzIhaxL7Y";
final coords = FlexiblePolyline.decode(encoded);
expect(coords.isNotEmpty, true);
});
}
Loading

0 comments on commit 71c34cb

Please sign in to comment.