diff --git a/craft_platforms/__init__.py b/craft_platforms/__init__.py index 26d41bd2..6ddf2a85 100644 --- a/craft_platforms/__init__.py +++ b/craft_platforms/__init__.py @@ -15,7 +15,12 @@ # with this program. If not, see . """Package base for craft_platforms.""" -from ._architectures import DebianArchitecture, parse_base_and_architecture +from ._architectures import ( + DebianArchitecture, + GoArchitectureValue, + GoArchitecture, + parse_base_and_architecture, +) from ._build import get_build_plan from ._buildinfo import BuildInfo from . import charm, rock, snap diff --git a/craft_platforms/_architectures.py b/craft_platforms/_architectures.py index e753408f..b3662e46 100644 --- a/craft_platforms/_architectures.py +++ b/craft_platforms/_architectures.py @@ -19,7 +19,7 @@ import enum import platform -from typing import Literal, Optional, Tuple, Union +from typing import Literal, NamedTuple, Optional, Tuple, Union from typing_extensions import Self @@ -70,6 +70,36 @@ def to_platform_arch(self) -> str: """ return _ARCH_TRANSLATIONS_DEB_TO_PLATFORM.get(self.value, self.value) + def to_go(self) -> GoArchitecture: + """Convert this architecture value to a Go architecture.""" + return GoArchitecture[self.name] + + +class GoArchitectureValue(NamedTuple): + """A value for a single Go architecture.""" + + arch: str + variant: str | None = None + + +class GoArchitecture(enum.Enum): + """An architecture for Go. + + The names of these values are Debian architecture names; the values are Go arches. + """ + + AMD64 = GoArchitectureValue("amd64") + ARM64 = GoArchitectureValue("arm64", "v8") + ARMHF = GoArchitectureValue("arm", "v7") + I386 = GoArchitectureValue("386") + PPC64EL = GoArchitectureValue("ppc64le") + RISCV64 = GoArchitectureValue("riscv64") + S390X = GoArchitectureValue("s390x") + + def to_debian(self) -> DebianArchitecture: + """Convert this go architecture to a Debian architecture.""" + return DebianArchitecture[self.name] + # architecture translations from the platform syntax to the deb/snap syntax _ARCH_TRANSLATIONS_PLATFORM_TO_DEB = { diff --git a/tests/unit/test_architectures.py b/tests/unit/test_architectures.py index 4a9c2d2e..183b1637 100644 --- a/tests/unit/test_architectures.py +++ b/tests/unit/test_architectures.py @@ -19,6 +19,7 @@ import pytest from craft_platforms import DebianArchitecture, DistroBase, parse_base_and_architecture +from craft_platforms._architectures import GoArchitecture from craft_platforms.test import strategies from hypothesis import given @@ -94,6 +95,24 @@ def test_fuzz_parse_base_and_architecture(base, arch): assert out_arch == arch +@pytest.mark.parametrize( + ("go_arch", "debian_arch"), + [ + # All pairs + *zip(GoArchitecture, DebianArchitecture), + # Specific examples + (GoArchitecture.PPC64EL, DebianArchitecture.PPC64EL), + (GoArchitecture.RISCV64, DebianArchitecture.RISCV64), + ], +) +def test_convert_arch(go_arch: GoArchitecture, debian_arch: DebianArchitecture): + assert go_arch.to_debian() == debian_arch + assert debian_arch.to_go() == go_arch + + assert debian_arch.to_go().to_debian() == debian_arch + assert go_arch.to_debian().to_go() == go_arch + + @pytest.mark.parametrize("deb_arch", list(DebianArchitecture)) def test_debian_architecture_repr(deb_arch: DebianArchitecture): """Test that the repr of DebianArchitectures is the repr of their string value."""