Skip to content

Commit 364082b

Browse files
james-garner-canonicaldimaqq
authored andcommitted
fix: don't use lexical sorting for version numbers in codegen
Previously, python-libjuju iterated over schemas keyed by their version string (e.g. '3.1.9') using lexical sorting. For a given facade version, a definition in a higher versioned schema was intended to overwrite any prior definition saved (see generate_facades function in facade.py). With lexical sorting, '3.1.10' would be sorted in between '3.1.1' and '3.1.2', which would not lead to the desired behaviour. This commit fixes this problem by using a tuple of integers as the sorting key. A special case is requried for the version string 'latest', and we use (9000, 9000, 9000).
1 parent c58ec9d commit 364082b

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

juju/client/facade.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from collections import defaultdict
1414
from glob import glob
1515
from pathlib import Path
16-
from typing import Any, Mapping, Sequence, TypeVar
16+
from typing import Any, Dict, List, Mapping, Sequence, Tuple, TypeVar
1717

1818
import typing_inspect
1919

@@ -926,11 +926,22 @@ def generate_definitions(schemas):
926926
return definitions
927927

928928

929-
def generate_facades(schemas):
929+
def sortable_schema_version(version_string: str) -> Tuple[int, int, int]:
930+
"""Return a sorting key in the form (major, minor, micro) from a version string."""
931+
# 'latest' is special cased in load_schemas and should come last
932+
if version_string == 'latest':
933+
return (9000, 9000, 9000)
934+
# raise ValueError if string isn't in the format A.B.C
935+
major, minor, micro = version_string.split('.')
936+
# raise ValueError if major, minor, and micro aren't int strings
937+
return (int(major), int(minor), int(micro))
938+
939+
940+
def generate_facades(schemas: Dict[str, List[Schema]]) -> Dict[str, Dict[int, codegen.Capture]]:
930941
captures = defaultdict(codegen.Capture)
931942

932943
# Build the Facade classes
933-
for juju_version in sorted(schemas.keys()):
944+
for juju_version in sorted(schemas.keys(), key=sortable_schema_version):
934945
for schema in schemas[juju_version]:
935946
cls, source = buildFacade(schema)
936947
cls_name = "{}Facade".format(schema.name)

0 commit comments

Comments
 (0)