-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fix: sort document reference by long type id #14248
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,8 +150,18 @@ class BasePath { | |
std::equal(begin(), end(), potential_child.begin()); | ||
} | ||
|
||
/** | ||
* Compares the current path against another Path object. Paths are compared | ||
* segment by segment, prioritizing numeric IDs (e.g., "__id123__") in numeric | ||
* ascending order, followed by string segments in lexicographical order. | ||
*/ | ||
util::ComparisonResult CompareTo(const T& rhs) const { | ||
return util::CompareContainer(segments_, rhs.segments_); | ||
size_t min_size = std::min(size(), rhs.size()); | ||
for (size_t i = 0; i < min_size; ++i) { | ||
auto cmp = compareSegments(segments_[i], rhs.segments_[i]); | ||
if (!util::Same(cmp)) return cmp; | ||
} | ||
return util::Compare(size(), rhs.size()); | ||
} | ||
|
||
friend bool operator==(const BasePath& lhs, const BasePath& rhs) { | ||
|
@@ -174,6 +184,31 @@ class BasePath { | |
|
||
private: | ||
SegmentsT segments_; | ||
|
||
static util::ComparisonResult compareSegments(const std::string& lhs, | ||
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. C++ style guide suggests function names to start with a capital letter ( |
||
const std::string& rhs) { | ||
bool isLhsNumeric = isNumericId(lhs); | ||
bool isRhsNumeric = isNumericId(rhs); | ||
|
||
if (isLhsNumeric && !isRhsNumeric) { | ||
return util::ComparisonResult::Ascending; | ||
} else if (!isLhsNumeric && isRhsNumeric) { | ||
return util::ComparisonResult::Descending; | ||
} else if (isLhsNumeric && isRhsNumeric) { | ||
return util::Compare(extractNumericId(lhs), extractNumericId(rhs)); | ||
} else { | ||
return util::Compare(lhs, rhs); | ||
} | ||
} | ||
|
||
static bool isNumericId(const std::string& segment) { | ||
return segment.substr(0, 4) == "__id" && | ||
segment.substr(segment.size() - 2) == "__"; | ||
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 may want to do: return
segment.size() > 6 &&
segment.substr(0, 4) == "__id" &&
segment.substr(segment.size() - 2) == "__"; 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. by adding unit tests I mean testing that the output of |
||
} | ||
|
||
static int64_t extractNumericId(const std::string& segment) { | ||
return std::stol(segment.substr(4, segment.size() - 2)); | ||
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.
|
||
} | ||
}; | ||
|
||
} // namespace impl | ||
|
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.
CompareContainer is a generic method that compares items in 2 T& variables by iteration, and it is only used here in base_path. The sorting order is unique to base_path(specifically resource_path), makes more sense to add the logic here instead of modifying the CompareContainer function.