Skip to content

Commit

Permalink
release: 3.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Jun 1, 2023
1 parent bb21c71 commit b549abf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 28 deletions.
4 changes: 4 additions & 0 deletions slang/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.17.0

- fix: setLocale does not work when Locale enum is from two packages (by [@fzyzcjy](https://github.com/fzyzcjy))

## 3.16.2

- fix: handle dynamic keys when `fallback_strategy: base_locale` is used
Expand Down
20 changes: 15 additions & 5 deletions slang/lib/builder/utils/map_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ class MapUtils {

/// Updates an existing entry at [path].
/// Modifiers are ignored and should be not included in the [path].
/// The [update] function is called with the key and value of the entry.
/// The return value of the [update] function is used to update the entry.
///
/// The [update] function uses the key and value of the entry
/// and returns the result to update the entry.
///
/// It updates the entry in place.
static void updateEntry({
/// Returns true, if the entry was updated.
static bool updateEntry({
required Map<String, dynamic> map,
required String path,
required MapEntry<String, dynamic> Function(String key, Object path) update,
Expand All @@ -142,7 +145,8 @@ class MapUtils {
return key == subPath;
});
if (entryIndex == -1) {
throw 'The leaf "$path" cannot be updated because it does not exist.';
// The leaf cannot be updated because it does not exist.
return false;
}
final MapEntry<String, dynamic> currEntry = entryList[entryIndex];

Expand All @@ -160,13 +164,19 @@ class MapUtils {
currMap[updated.key] = updated.value;
currMap.addEntries(entryList.skip(entryIndex + 1));
}

return true;
} else {
if (currEntry.value is! Map<String, dynamic>) {
throw 'The leaf "$path" cannot be updated because "$subPath" is not a map.';
// The leaf cannot be updated because "subPath" is not a map.
return false;
}
currMap = currEntry.value;
}
}

// This should never be reached.
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion slang/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: slang
description: Localization / Internationalization (i18n) solution. Use JSON, YAML or CSV files to create typesafe translations via source generation.
version: 3.16.2
version: 3.17.0
repository: https://github.com/Tienisto/slang

environment:
Expand Down
33 changes: 15 additions & 18 deletions slang/test/unit/utils/map_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ void main() {
},
};

MapUtils.updateEntry(
final result = MapUtils.updateEntry(
map: map,
path: 'a.b.c',
update: (key, value) => MapEntry(key, (value as int) * 2),
);

expect(result, true);
expect(map['a']!['b']!['c'], 84);
});

Expand All @@ -175,12 +176,13 @@ void main() {
},
};

MapUtils.updateEntry(
final result = MapUtils.updateEntry(
map: map,
path: 'a.b.c',
update: (key, value) => MapEntry('d', (value as int) * 2),
);

expect(result, true);
expect(map['a']!['b']!['d'], 84);
expect(map['a']!['b']!['c'], null);
});
Expand All @@ -196,12 +198,13 @@ void main() {
},
};

MapUtils.updateEntry(
final result = MapUtils.updateEntry(
map: map,
path: 'a.b.c1',
update: (key, value) => MapEntry('d', (value as int) * 2),
);

expect(result, true);
expect(map['a']!['b']!['d'], 8);
expect(map['a']!['b']!.keys, ['c0', 'd', 'c2']);
});
Expand All @@ -215,17 +218,18 @@ void main() {
},
};

MapUtils.updateEntry(
final result = MapUtils.updateEntry(
map: map,
path: 'a.b.c',
update: (key, value) => MapEntry('d(nice)', (value as int) * 2),
);

expect(result, true);
expect(map['a']!['b(rich)']!['d(nice)'], 84);
expect(map['a']!['b(rich)']!['c(ignore)'], null);
});

test('should throw an error if the path does not exist', () {
test('should return false if the path does not exist', () {
final map = {
'a': {
'b': {
Expand All @@ -234,20 +238,13 @@ void main() {
},
};

expect(
() => MapUtils.updateEntry(
map: map,
path: 'a.b.d',
update: (key, value) => MapEntry(key, value),
),
throwsA(
isA<String>().having(
(s) => s,
'error message',
'The leaf "a.b.d" cannot be updated because it does not exist.',
),
),
final result = MapUtils.updateEntry(
map: map,
path: 'a.b.d',
update: (key, value) => MapEntry(key, value),
);

expect(result, false);
});
});
}
6 changes: 6 additions & 0 deletions slang_build_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.17.0

- fix: setLocale does not work when Locale enum is from two packages (by [@fzyzcjy](https://github.com/fzyzcjy))
- fix: `slang outdated` should skip missing translations instead of throwing an error
- Bump `slang` to `3.17.0`

## 3.16.0

- Bump `slang` to `3.16.0`
Expand Down
4 changes: 2 additions & 2 deletions slang_build_runner/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: slang_build_runner
description: build_runner integration for slang. This library ensures that slang is recognized by build_runner.
version: 3.16.0
version: 3.17.0
repository: https://github.com/Tienisto/slang

environment:
Expand All @@ -11,4 +11,4 @@ dependencies:
glob: ^2.0.2

# Use tight version to ensure that all features are available
slang: '>=3.16.0 <3.17.0'
slang: '>=3.17.0 <3.18.0'
4 changes: 4 additions & 0 deletions slang_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.17.0

- Bump `slang` to `3.17.0`

## 3.16.0

- Bump `slang` to `3.16.0`
Expand Down
4 changes: 2 additions & 2 deletions slang_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: slang_flutter
description: Flutter support for slang. This library provides helpful Flutter API.
version: 3.16.0
version: 3.17.0
repository: https://github.com/Tienisto/slang

environment:
Expand All @@ -11,4 +11,4 @@ dependencies:
sdk: flutter

# Use tight version to ensure that all features are available
slang: '>=3.16.0 <3.17.0'
slang: '>=3.17.0 <3.18.0'

0 comments on commit b549abf

Please sign in to comment.