hst/fs: remove type method
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m3s
Test / Hakurei (push) Successful in 3m7s
Test / Hpkg (push) Successful in 3m51s
Test / Sandbox (race detector) (push) Successful in 4m14s
Test / Hakurei (race detector) (push) Successful in 4m54s
Test / Flake checks (push) Successful in 1m28s
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m3s
Test / Hakurei (push) Successful in 3m7s
Test / Hpkg (push) Successful in 3m51s
Test / Sandbox (race detector) (push) Successful in 4m14s
Test / Hakurei (race detector) (push) Successful in 4m54s
Test / Flake checks (push) Successful in 1m28s
Having a method that returns the canonical string representation of its type seemed like a much better idea for an implementation that never made it to staging. Remove it here and clean up marshal type assertions. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
58
hst/fs.go
58
hst/fs.go
@@ -11,8 +11,6 @@ import (
|
||||
|
||||
// FilesystemConfig is an abstract representation of a mount point.
|
||||
type FilesystemConfig interface {
|
||||
// Type returns the type of this mount point.
|
||||
Type() string
|
||||
// Valid returns whether the configuration is valid.
|
||||
Valid() bool
|
||||
// Target returns the pathname of the mount point in the container.
|
||||
@@ -34,12 +32,8 @@ type FSTypeError string
|
||||
|
||||
func (f FSTypeError) Error() string { return fmt.Sprintf("invalid filesystem type %q", string(f)) }
|
||||
|
||||
// FSImplError is returned when the underlying struct of [FilesystemConfig] does not match
|
||||
// what [FilesystemConfig.Type] claims to be.
|
||||
type FSImplError struct {
|
||||
Type string
|
||||
Value FilesystemConfig
|
||||
}
|
||||
// FSImplError is returned for unsupported implementations of [FilesystemConfig].
|
||||
type FSImplError struct{ Value FilesystemConfig }
|
||||
|
||||
func (f FSImplError) Error() string {
|
||||
implType := reflect.TypeOf(f.Value)
|
||||
@@ -53,57 +47,49 @@ func (f FSImplError) Error() string {
|
||||
} else {
|
||||
name += "nil"
|
||||
}
|
||||
return fmt.Sprintf("implementation %s is not %s", name, f.Type)
|
||||
return fmt.Sprintf("implementation %s not supported", name)
|
||||
}
|
||||
|
||||
// FilesystemConfigJSON is the [json] adapter for [FilesystemConfig].
|
||||
type FilesystemConfigJSON struct {
|
||||
FilesystemConfig
|
||||
}
|
||||
type FilesystemConfigJSON struct{ FilesystemConfig }
|
||||
|
||||
// Valid returns whether the [FilesystemConfigJSON] is valid.
|
||||
func (f *FilesystemConfigJSON) Valid() bool {
|
||||
return f != nil && f.FilesystemConfig != nil && f.FilesystemConfig.Valid()
|
||||
}
|
||||
|
||||
// fsType holds the string representation of a [FilesystemConfig]'s concrete type.
|
||||
type fsType struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func (f *FilesystemConfigJSON) MarshalJSON() ([]byte, error) {
|
||||
if f == nil || f.FilesystemConfig == nil {
|
||||
return nil, ErrFSNull
|
||||
}
|
||||
var v any
|
||||
t := f.Type()
|
||||
switch t {
|
||||
case FilesystemBind:
|
||||
if ct, ok := f.FilesystemConfig.(*FSBind); !ok {
|
||||
return nil, FSImplError{t, f.FilesystemConfig}
|
||||
} else {
|
||||
v = &struct {
|
||||
Type string `json:"type"`
|
||||
*FSBind
|
||||
}{FilesystemBind, ct}
|
||||
}
|
||||
switch cv := f.FilesystemConfig.(type) {
|
||||
case *FSBind:
|
||||
v = &struct {
|
||||
fsType
|
||||
*FSBind
|
||||
}{fsType{FilesystemBind}, cv}
|
||||
|
||||
case FilesystemEphemeral:
|
||||
if ct, ok := f.FilesystemConfig.(*FSEphemeral); !ok {
|
||||
return nil, FSImplError{t, f.FilesystemConfig}
|
||||
} else {
|
||||
v = &struct {
|
||||
Type string `json:"type"`
|
||||
*FSEphemeral
|
||||
}{FilesystemEphemeral, ct}
|
||||
}
|
||||
case *FSEphemeral:
|
||||
v = &struct {
|
||||
fsType
|
||||
*FSEphemeral
|
||||
}{fsType{FilesystemEphemeral}, cv}
|
||||
|
||||
default:
|
||||
return nil, FSTypeError(t)
|
||||
return nil, FSImplError{f.FilesystemConfig}
|
||||
}
|
||||
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (f *FilesystemConfigJSON) UnmarshalJSON(data []byte) error {
|
||||
t := new(struct {
|
||||
Type string `json:"type"`
|
||||
})
|
||||
t := new(fsType)
|
||||
if err := json.Unmarshal(data, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user