Platforms: Add From/ToProto helpers for types

Helpers to convert from a slice of platforms to our protobuf representation
and vice-versa appear a couple times. It seems sane to just expose this facility
in the platforms pkg.

Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
Danny Canter
2023-06-28 15:12:30 -07:00
parent b3ab1f26c4
commit f3b7436b61
4 changed files with 33 additions and 46 deletions

View File

@@ -116,6 +116,7 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
)
@@ -262,3 +263,30 @@ func Normalize(platform specs.Platform) specs.Platform {
return platform
}
// ToProto converts from a slice of [Platform] to a slice of
// the protobuf definition [types.Platform].
func ToProto(platforms []Platform) []*types.Platform {
ap := make([]*types.Platform, len(platforms))
for i := range platforms {
p := types.Platform{
OS: platforms[i].OS,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
}
ap[i] = &p
}
return ap
}
// FromProto converts a slice of the protobuf definition [types.Platform]
// to a slice of [Platform].
func FromProto(platforms []*types.Platform) []Platform {
op := make([]Platform, len(platforms))
for i := range platforms {
op[i].OS = platforms[i].OS
op[i].Architecture = platforms[i].Architecture
op[i].Variant = platforms[i].Variant
}
return op
}