Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] many: add types.Option generic and use in customizations #860

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/osbuild-playground/my-container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"math/rand"

"github.com/osbuild/images/internal/types"
"github.com/osbuild/images/pkg/artifact"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/platform"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (img *MyContainer) InstantiateManifest(m *manifest.Manifest,
os.ExtraBasePackages = []string{"@core"}
os.OSCustomizations.Language = "en_US.UTF-8"
os.OSCustomizations.Hostname = "my-host"
os.OSCustomizations.Timezone = "UTC"
os.OSCustomizations.Timezone = types.Some("UTC")

// create an OCI container containing the OS tree created above
container := manifest.NewOCIContainer(build, os)
Expand Down
8 changes: 8 additions & 0 deletions internal/types/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright 2021 Taiki Kawakami (a.k.a. moznion) https://moznion.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

117 changes: 117 additions & 0 deletions internal/types/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package types

import (
"bytes"
"encoding/json"
"fmt"
)

// Option is a more constrained subset of the code in
// https://github.com/moznion/go-optional
//
// It is not using an external go-optional lib directly because none
// has toml unmarshal support and also because there is no support for
// complex types in UnmarshalTOML (it only gives a single
// reflect.Value of type any) so our optional "lib" must come with
// limitations for this.
//
// Unfortunately there is no way I could find to import the code and
// limit the supported types. So this is a copy of the subset.
// Fortunatly the code is small, targeted and easy to follow so it
// should not be too bad.

type OptionTomlTypes interface {
~int | ~bool | ~string
}

// Option is a subset of github.com/moznion/go-optional for use with toml

// Option is a data type that must be Some (i.e. having a value) or None (i.e. doesn't have a value).
// This type implements database/sql/driver.Valuer and database/sql.Scanner.
type Option[T OptionTomlTypes] []T
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, we could call this Optional too which I actually slightly prefer but then Option is what rust uses and lots of people are familar with this terminology now.


const (
value = iota
)

// Some is a function to make an Option type value with the actual value.
func Some[T OptionTomlTypes](v T) Option[T] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, we could call this differently, e.g. types.NewOptional("foo") or somesuch but again rust is the inspiration and it seems nice to keep close to their terminology as many people will be familiar with it.

return Option[T]{
value: v,
}
}

// None is a function to make an Option type value that doesn't have a value.
func None[T OptionTomlTypes]() Option[T] {
return nil
}

// IsNone returns True if the Option *doesn't* have a value
func (o Option[T]) IsNone() bool {
return o == nil
}

// IsSome returns whether the Option has a value or not.
func (o Option[T]) IsSome() bool {
return o != nil
}

// Unwrap returns the value regardless of Some/None status.
// If the Option value is Some, this method returns the actual value.
// On the other hand, if the Option value is None, this method returns the *default* value according to the type.
func (o Option[T]) Unwrap() T {
if o.IsNone() {
var defaultValue T
return defaultValue
}
return o[value]
}

// TakeOr returns the actual value if the Option has a value.
// On the other hand, this returns fallbackValue.
func (o Option[T]) TakeOr(fallbackValue T) T {
if o.IsNone() {
return fallbackValue
}
return o[value]
}

var jsonNull = []byte("null")

func (o Option[T]) MarshalJSON() ([]byte, error) {
if o.IsNone() {
return jsonNull, nil
}

marshal, err := json.Marshal(o.Unwrap())
if err != nil {
return nil, err
}
return marshal, nil
}

func (o *Option[T]) UnmarshalJSON(data []byte) error {
if len(data) <= 0 || bytes.Equal(data, jsonNull) {
*o = None[T]()
return nil
}

var v T
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
*o = Some(v)

return nil
}

// not part of github.com/moznion/go-optional
func (o *Option[T]) UnmarshalTOML(data any) error {
b, ok := data.(T)
if !ok {
return fmt.Errorf("cannot use %[1]v (%[1]T) as bool", data)
}
*o = Some(b)
return nil
}
80 changes: 80 additions & 0 deletions internal/types/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package types_test

import (
"testing"

"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"

"github.com/osbuild/images/internal/types"
)

type s1 struct {
Name string `toml:"name"`
Sub s2 `toml:"sub"`
}

type s2 struct {
ValBool types.Option[bool] `toml:"val-bool"`
ValString types.Option[string] `toml:"val-string"`
ValString2 types.Option[string] `toml:"val-string2"`
}

func TestTomlParseOption(t *testing.T) {
testTomlStr := `
name = "some-name"
[sub]
val-bool = true
val-string = "opt-string"
`

var bp s1
err := toml.Unmarshal([]byte(testTomlStr), &bp)
assert.NoError(t, err)
assert.Equal(t, bp.Name, "some-name")
assert.Equal(t, types.Some(true), bp.Sub.ValBool)
assert.Equal(t, types.Some("opt-string"), bp.Sub.ValString)
assert.EqualValues(t, types.None[string](), bp.Sub.ValString2)
}

func TestTomlParseOptionBad(t *testing.T) {
testTomlStr := `
[sub]
val-bool = 1234
`

var bp s1
err := toml.Unmarshal([]byte(testTomlStr), &bp)
assert.ErrorContains(t, err, "cannot use 1234 (int64) as bool")
}

// taken from https://github.com/moznion/go-optional
func TestOption_IsNone(t *testing.T) {
assert.True(t, types.None[int]().IsNone())
assert.False(t, types.Some[int](123).IsNone())

var nilValue types.Option[int] = nil
assert.True(t, nilValue.IsNone())
}

func TestOption_IsSome(t *testing.T) {
assert.False(t, types.None[int]().IsSome())
assert.True(t, types.Some[int](123).IsSome())

var nilValue types.Option[int] = nil
assert.False(t, nilValue.IsSome())
}

func TestOption_Unwrap(t *testing.T) {
assert.Equal(t, "foo", types.Some[string]("foo").Unwrap())
assert.Equal(t, "", types.None[string]().Unwrap())
assert.Equal(t, false, types.None[bool]().Unwrap())
}

func TestOption_TakeOr(t *testing.T) {
v := types.Some[int](123).TakeOr(666)
assert.Equal(t, 123, v)

v = types.None[int]().TakeOr(666)
assert.Equal(t, 666, v)
}
8 changes: 6 additions & 2 deletions pkg/blueprint/blueprint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Package blueprint contains primitives for representing weldr blueprints
package blueprint

import (
"github.com/osbuild/images/internal/types"
)

// A Blueprint is a high-level description of an image.
type Blueprint struct {
Name string `json:"name" toml:"name"`
Expand Down Expand Up @@ -32,8 +36,8 @@ type Container struct {
Source string `json:"source" toml:"source"`
Name string `json:"name,omitempty" toml:"name,omitempty"`

TLSVerify *bool `json:"tls-verify,omitempty" toml:"tls-verify,omitempty"`
LocalStorage bool `json:"local-storage,omitempty" toml:"local-storage,omitempty"`
TLSVerify types.Option[bool] `json:"tls-verify,omitempty" toml:"tls-verify,omitempty"`
LocalStorage bool `json:"local-storage,omitempty" toml:"local-storage,omitempty"`
}

// packages, modules, and groups all resolve to rpm packages right now. This
Expand Down
26 changes: 26 additions & 0 deletions pkg/blueprint/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,29 @@ func TestKernelNameCustomization(t *testing.T) {
}
}
}

func TestBlueprintParseSetHostnameOptional(t *testing.T) {
blueprint := `
name = "test"

[customizations]
hostname = "my-hostname"
`

var bp Blueprint
err := toml.Unmarshal([]byte(blueprint), &bp)
require.Nil(t, err)
assert.Equal(t, bp.Name, "test")
assert.Equal(t, "my-hostname", bp.Customizations.GetHostname().Unwrap())

blueprint = `{
"name": "test",
"customizations": {
"hostname": "my-hostname"
}
}`
err = json.Unmarshal([]byte(blueprint), &bp)
require.Nil(t, err)
assert.Equal(t, bp.Name, "test")
assert.Equal(t, "my-hostname", bp.Customizations.GetHostname().Unwrap())
}
45 changes: 23 additions & 22 deletions pkg/blueprint/customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"slices"
"strings"

"github.com/osbuild/images/internal/types"
"github.com/osbuild/images/pkg/customizations/anaconda"
)

type Customizations struct {
Hostname *string `json:"hostname,omitempty" toml:"hostname,omitempty"`
Hostname types.Option[string] `json:"hostname,omitempty" toml:"hostname,omitempty"`
Kernel *KernelCustomization `json:"kernel,omitempty" toml:"kernel,omitempty"`
SSHKey []SSHKeyCustomization `json:"sshkey,omitempty" toml:"sshkey,omitempty"`
User []UserCustomization `json:"user,omitempty" toml:"user,omitempty"`
Expand All @@ -27,7 +28,7 @@ type Customizations struct {
Directories []DirectoryCustomization `json:"directories,omitempty" toml:"directories,omitempty"`
Files []FileCustomization `json:"files,omitempty" toml:"files,omitempty"`
Repositories []RepositoryCustomization `json:"repositories,omitempty" toml:"repositories,omitempty"`
FIPS *bool `json:"fips,omitempty" toml:"fips,omitempty"`
FIPS types.Option[bool] `json:"fips,omitempty" toml:"fips,omitempty"`
ContainersStorage *ContainerStorageCustomization `json:"containers-storage,omitempty" toml:"containers-storage,omitempty"`
Installer *InstallerCustomization `json:"installer,omitempty" toml:"installer,omitempty"`
RPM *RPMCustomization `json:"rpm,omitempty" toml:"rpm,omitempty"`
Expand Down Expand Up @@ -68,17 +69,17 @@ type SSHKeyCustomization struct {
}

type UserCustomization struct {
Name string `json:"name" toml:"name"`
Description *string `json:"description,omitempty" toml:"description,omitempty"`
Password *string `json:"password,omitempty" toml:"password,omitempty"`
Key *string `json:"key,omitempty" toml:"key,omitempty"`
Home *string `json:"home,omitempty" toml:"home,omitempty"`
Shell *string `json:"shell,omitempty" toml:"shell,omitempty"`
Groups []string `json:"groups,omitempty" toml:"groups,omitempty"`
UID *int `json:"uid,omitempty" toml:"uid,omitempty"`
GID *int `json:"gid,omitempty" toml:"gid,omitempty"`
ExpireDate *int `json:"expiredate,omitempty" toml:"expiredate,omitempty"`
ForcePasswordReset *bool `json:"force_password_reset,omitempty" toml:"force_password_reset,omitempty"`
Name string `json:"name" toml:"name"`
Description types.Option[string] `json:"description,omitempty" toml:"description,omitempty"`
Password types.Option[string] `json:"password,omitempty" toml:"password,omitempty"`
Key types.Option[string] `json:"key,omitempty" toml:"key,omitempty"`
Home types.Option[string] `json:"home,omitempty" toml:"home,omitempty"`
Shell types.Option[string] `json:"shell,omitempty" toml:"shell,omitempty"`
Groups []string `json:"groups,omitempty" toml:"groups,omitempty"`
UID types.Option[int] `json:"uid,omitempty" toml:"uid,omitempty"`
GID types.Option[int] `json:"gid,omitempty" toml:"gid,omitempty"`
ExpireDate types.Option[int] `json:"expiredate,omitempty" toml:"expiredate,omitempty"`
ForcePasswordReset types.Option[bool] `json:"force_password_reset,omitempty" toml:"force_password_reset,omitempty"`
}

type GroupCustomization struct {
Expand All @@ -87,8 +88,8 @@ type GroupCustomization struct {
}

type TimezoneCustomization struct {
Timezone *string `json:"timezone,omitempty" toml:"timezone,omitempty"`
NTPServers []string `json:"ntpservers,omitempty" toml:"ntpservers,omitempty"`
Timezone types.Option[string] `json:"timezone,omitempty" toml:"timezone,omitempty"`
NTPServers []string `json:"ntpservers,omitempty" toml:"ntpservers,omitempty"`
}

type LocaleCustomization struct {
Expand Down Expand Up @@ -197,7 +198,7 @@ func (c *Customizations) CheckAllowed(allowed ...string) error {
return nil
}

func (c *Customizations) GetHostname() *string {
func (c *Customizations) GetHostname() types.Option[string] {
if c == nil {
return nil
}
Expand All @@ -217,7 +218,7 @@ func (c *Customizations) GetPrimaryLocale() (*string, *string) {
return &c.Locale.Languages[0], c.Locale.Keyboard
}

func (c *Customizations) GetTimezoneSettings() (*string, []string) {
func (c *Customizations) GetTimezoneSettings() (types.Option[string], []string) {
if c == nil {
return nil, nil
}
Expand All @@ -240,7 +241,7 @@ func (c *Customizations) GetUsers() []UserCustomization {
keyc := c.SSHKey[idx]
users = append(users, UserCustomization{
Name: keyc.User,
Key: &keyc.Key,
Key: types.Some(keyc.Key),
})
}
}
Expand All @@ -252,8 +253,8 @@ func (c *Customizations) GetUsers() []UserCustomization {
for idx := range users {
u := users[idx]
if u.Home != nil {
homedir := strings.TrimRight(*u.Home, "/")
u.Home = &homedir
homedir := strings.TrimRight(u.Home.Unwrap(), "/")
u.Home = types.Some(homedir)
users[idx] = u
}
}
Expand Down Expand Up @@ -383,10 +384,10 @@ func (c *Customizations) GetRepositories() ([]RepositoryCustomization, error) {
}

func (c *Customizations) GetFIPS() bool {
if c == nil || c.FIPS == nil {
if c == nil {
return false
}
return *c.FIPS
return c.FIPS.Unwrap()
}

func (c *Customizations) GetContainerStorage() *ContainerStorageCustomization {
Expand Down
Loading
Loading