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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user