-
Notifications
You must be signed in to change notification settings - Fork 186
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
Implement deepCopy as copying, instead of merge #742
Open
osa1
wants to merge
18
commits into
google:master
Choose a base branch
from
osa1:deepCopy
base: master
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.
Open
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f8d2953
Implement efficient deep copying
osa1 51ff5dd
Implement missing deepCopy methods
osa1 54c7b9b
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 244063f
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 f7495ff
Bug smesh
osa1 249b516
Simplify clone implementation
osa1 d839a4c
Simplify deepCopy implementation
osa1 ea515ef
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 3441ba0
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 862f742
Revert clone default implementation for now
osa1 091bb87
Actually revert GeneratedMessage.clone
osa1 fe68aaf
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 24c8c2b
Initialize cloned _FieldSet event plugin
osa1 47513bf
Remove a TODO
osa1 bb631b2
Use field types instead of value types in deepCopy impls
osa1 7f4f421
Remove unused {PbList,PbMap}.deepCopy
osa1 bc0a224
Merge remote-tracking branch 'origin/master' into deepCopy
osa1 12259f5
Fix formatting
osa1 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 |
---|---|---|
|
@@ -78,6 +78,9 @@ class _FieldSet { | |
: _values = _makeValueList(meta.byIndex.length), | ||
_oneofCases = meta.oneofs.isEmpty ? null : <int, int>{}; | ||
|
||
_FieldSet._(this._message, this._eventPlugin, this._values, this._extensions, | ||
this._unknownFields, this._frozenState, this._oneofCases); | ||
|
||
static List _makeValueList(int length) { | ||
if (length == 0) return _zeroList; | ||
return List.filled(length, null, growable: false); | ||
|
@@ -897,4 +900,35 @@ class _FieldSet { | |
|
||
_oneofCases?.addAll(original._oneofCases!); | ||
} | ||
|
||
_FieldSet deepCopy(GeneratedMessage message, {bool freeze = false}) { | ||
final values = _makeValueList(_values.length); | ||
|
||
for (var valueIdx = 0; valueIdx < _values.length; valueIdx += 1) { | ||
final value = _values[valueIdx]; | ||
if (value is PbMap) { | ||
values[valueIdx] = value.deepCopy(freeze: freeze); | ||
} else if (value is PbList) { | ||
values[valueIdx] = value.deepCopy(freeze: freeze); | ||
} else if (value is GeneratedMessage) { | ||
values[valueIdx] = value.deepCopy(freeze: freeze); | ||
} else { | ||
values[valueIdx] = value; | ||
} | ||
} | ||
|
||
final copy = _FieldSet._( | ||
message, | ||
null, // TODO: event plugin | ||
values, | ||
null, | ||
_unknownFields?.deepCopy(freeze: freeze), | ||
freeze, // TODO: Can we use `_frozenState` here? | ||
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. Same here |
||
_oneofCases == null ? null : Map.from(_oneofCases!), | ||
); | ||
|
||
copy._extensions = _extensions?.deepCopy(copy, freeze: freeze); | ||
|
||
return copy; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,13 +14,14 @@ class PbList<E> extends ListBase<E> { | |
final List<E> _wrappedList; | ||
final CheckFunc<E> _check; | ||
|
||
bool _isReadOnly = false; | ||
bool _isReadOnly; | ||
|
||
bool get isFrozen => _isReadOnly; | ||
|
||
PbList({CheckFunc<E> check = _checkNotNull}) | ||
: _wrappedList = <E>[], | ||
_check = check; | ||
_check = check, | ||
_isReadOnly = false; | ||
|
||
PbList.unmodifiable() | ||
: _wrappedList = const [], | ||
|
@@ -29,7 +30,10 @@ class PbList<E> extends ListBase<E> { | |
|
||
PbList.from(List from) | ||
: _wrappedList = List<E>.from(from), | ||
_check = _checkNotNull; | ||
_check = _checkNotNull, | ||
_isReadOnly = false; | ||
|
||
PbList._(this._wrappedList, this._check, this._isReadOnly); | ||
|
||
@override | ||
void add(E element) { | ||
|
@@ -200,4 +204,26 @@ class PbList<E> extends ListBase<E> { | |
static Never _readOnlyError(String methodName) { | ||
throw UnsupportedError("'$methodName' on a read-only list"); | ||
} | ||
|
||
PbList<E> deepCopy({bool freeze = false}) { | ||
// TODO: We know the capacity, is it possible to allocate a list with the right size? | ||
final wrappedList = <E>[]; | ||
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. You could possibly use |
||
|
||
for (final value in _wrappedList) { | ||
// TODO: I'm not sure why I need to cast to dynamic below. If `value : E` | ||
// and `value : PbMap`, then `value.deepCopy : E` | ||
if (value is PbMap) { | ||
wrappedList.add(value.deepCopy(freeze: freeze) as dynamic); | ||
} else if (value is PbList) { | ||
wrappedList.add(value.deepCopy(freeze: freeze) as dynamic); | ||
} else if (value is GeneratedMessage) { | ||
final GeneratedMessage message = value; | ||
wrappedList.add(message.deepCopy(freeze: freeze) as dynamic); | ||
} else { | ||
wrappedList.add(value); | ||
} | ||
} | ||
|
||
return PbList._(wrappedList, _check, freeze); | ||
} | ||
} |
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
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
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.
Please leave a LDAP or an issue number in todos.