Skip to content

Commit

Permalink
Merge pull request #77 from vfazio/vfazio-inline-first-sort
Browse files Browse the repository at this point in the history
Fix "first" sort for inline tables
  • Loading branch information
pappasam authored Nov 19, 2024
2 parents ba64498 + 9027991 commit 1d986cc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/examples/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ hosts = [
name = "Nail"
sku = 284758393
color = "gray"

[inline]
# Test "first" sorting for inline tables
c = { limit="<3.12", version ="4.12.2"}
4 changes: 4 additions & 0 deletions tests/examples/sorted/from-toml-lang-first.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ connection_max = 5000
enabled = true # Comment after a boolean
server = "192.168.1.1"

[inline]
# Test "first" sorting for inline tables
c = {version = "4.12.2", limit = "<3.12"}

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
Expand Down
4 changes: 4 additions & 0 deletions tests/examples/sorted/from-toml-lang-overrides.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ enabled = true # Comment after a boolean
ports = [8001, 8001, 8002]
server = "192.168.1.1"

[inline]
# Test "first" sorting for inline tables
c = {limit = "<3.12", version = "4.12.2"}

[owner]
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
Expand Down
4 changes: 4 additions & 0 deletions tests/examples/sorted/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ ports = [8001, 8001, 8002]
connection_max = 5000
enabled = true # Comment after a boolean

[inline]
# Test "first" sorting for inline tables
c = {limit = "<3.12", version = "4.12.2"}

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
Expand Down
1 change: 1 addition & 0 deletions tests/test_toml_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def test_sort_toml_is_str() -> None:
"sort_config_overrides": {
"database": SortOverrideConfiguration(first=["ports"]),
"owner": SortOverrideConfiguration(first=["name", "dob"]),
"inline.c": SortOverrideConfiguration(first=["version"]),
},
},
),
Expand Down
5 changes: 3 additions & 2 deletions toml_sort/tomlsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ def sort_keys(
"""

def sort_first(item: TomlSortItem) -> int:
if item.keys.base in sort_config.first:
return sort_config.first.index(item.keys.base.as_string())
for index, value in enumerate(sort_config.first):
if value == item.keys.base.key:
return index
return len(sort_config.first)

items = sorted(items, key=self.key_sort_func)
Expand Down

0 comments on commit 1d986cc

Please sign in to comment.