Added models

This commit is contained in:
henk
2026-05-14 12:37:44 +02:00
parent dd6d18678d
commit a94a3f187c
3 changed files with 103 additions and 1 deletions

97
internal/model/models.go Normal file
View File

@@ -0,0 +1,97 @@
package model
import (
"encoding/json"
"fmt"
)
type ProviderPlatformOs int
type ProviderPlatformArch int
const (
OsDarwin ProviderPlatformOs = iota
OsLinux
OsWindows
)
var (
osName = map[ProviderPlatformOs]string{
OsDarwin: "darwin",
OsLinux: "linux",
OsWindows: "windows",
}
osFromName = map[string]ProviderPlatformOs{
"darwin": OsDarwin,
"linux": OsLinux,
"windows": OsWindows,
}
)
func (os ProviderPlatformOs) String() string {
return osName[os]
}
const (
ArchAmd64 ProviderPlatformArch = iota
ArchArm64
)
var (
archName = map[ProviderPlatformArch]string{
ArchAmd64: "amd64",
ArchArm64: "arm",
}
archFromName = map[string]ProviderPlatformArch{
"amd64": ArchAmd64,
"arm": ArchArm64,
}
)
func (arch ProviderPlatformArch) String() string {
return archName[arch]
}
type ProviderPlatform struct {
Os ProviderPlatformOs
Arch ProviderPlatformArch
}
func (p ProviderPlatform) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Os string `json:"os"`
Arch string `json:"arch"`
}{
Os: p.Os.String(),
Arch: p.Arch.String(),
})
}
func (p *ProviderPlatform) UnmarshalJSON(data []byte) error {
var temp struct {
Os string `json:"os"`
Arch string `json:"arch"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
var ok bool
if p.Os, ok = osFromName[temp.Os]; !ok {
return fmt.Errorf("unknown OS: %s", temp.Os)
}
if p.Arch, ok = archFromName[temp.Arch]; !ok {
return fmt.Errorf("unknown architecture: %s", temp.Arch)
}
return nil
}
type ProviderVersion struct {
Version string `json:"version"`
Protocols []string `json:"protocols"`
Platforms []ProviderPlatform `json:"platforms"`
}