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:
parent
ba3227bf15
commit
430991c39b
58
hst/fs.go
58
hst/fs.go
@ -11,8 +11,6 @@ import (
|
|||||||
|
|
||||||
// FilesystemConfig is an abstract representation of a mount point.
|
// FilesystemConfig is an abstract representation of a mount point.
|
||||||
type FilesystemConfig interface {
|
type FilesystemConfig interface {
|
||||||
// Type returns the type of this mount point.
|
|
||||||
Type() string
|
|
||||||
// Valid returns whether the configuration is valid.
|
// Valid returns whether the configuration is valid.
|
||||||
Valid() bool
|
Valid() bool
|
||||||
// Target returns the pathname of the mount point in the container.
|
// 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)) }
|
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
|
// FSImplError is returned for unsupported implementations of [FilesystemConfig].
|
||||||
// what [FilesystemConfig.Type] claims to be.
|
type FSImplError struct{ Value FilesystemConfig }
|
||||||
type FSImplError struct {
|
|
||||||
Type string
|
|
||||||
Value FilesystemConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FSImplError) Error() string {
|
func (f FSImplError) Error() string {
|
||||||
implType := reflect.TypeOf(f.Value)
|
implType := reflect.TypeOf(f.Value)
|
||||||
@ -53,57 +47,49 @@ func (f FSImplError) Error() string {
|
|||||||
} else {
|
} else {
|
||||||
name += "nil"
|
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].
|
// FilesystemConfigJSON is the [json] adapter for [FilesystemConfig].
|
||||||
type FilesystemConfigJSON struct {
|
type FilesystemConfigJSON struct{ FilesystemConfig }
|
||||||
FilesystemConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// Valid returns whether the [FilesystemConfigJSON] is valid.
|
// Valid returns whether the [FilesystemConfigJSON] is valid.
|
||||||
func (f *FilesystemConfigJSON) Valid() bool {
|
func (f *FilesystemConfigJSON) Valid() bool {
|
||||||
return f != nil && f.FilesystemConfig != nil && f.FilesystemConfig.Valid()
|
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) {
|
func (f *FilesystemConfigJSON) MarshalJSON() ([]byte, error) {
|
||||||
if f == nil || f.FilesystemConfig == nil {
|
if f == nil || f.FilesystemConfig == nil {
|
||||||
return nil, ErrFSNull
|
return nil, ErrFSNull
|
||||||
}
|
}
|
||||||
var v any
|
var v any
|
||||||
t := f.Type()
|
switch cv := f.FilesystemConfig.(type) {
|
||||||
switch t {
|
case *FSBind:
|
||||||
case FilesystemBind:
|
v = &struct {
|
||||||
if ct, ok := f.FilesystemConfig.(*FSBind); !ok {
|
fsType
|
||||||
return nil, FSImplError{t, f.FilesystemConfig}
|
*FSBind
|
||||||
} else {
|
}{fsType{FilesystemBind}, cv}
|
||||||
v = &struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
*FSBind
|
|
||||||
}{FilesystemBind, ct}
|
|
||||||
}
|
|
||||||
|
|
||||||
case FilesystemEphemeral:
|
case *FSEphemeral:
|
||||||
if ct, ok := f.FilesystemConfig.(*FSEphemeral); !ok {
|
v = &struct {
|
||||||
return nil, FSImplError{t, f.FilesystemConfig}
|
fsType
|
||||||
} else {
|
*FSEphemeral
|
||||||
v = &struct {
|
}{fsType{FilesystemEphemeral}, cv}
|
||||||
Type string `json:"type"`
|
|
||||||
*FSEphemeral
|
|
||||||
}{FilesystemEphemeral, ct}
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, FSTypeError(t)
|
return nil, FSImplError{f.FilesystemConfig}
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Marshal(v)
|
return json.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FilesystemConfigJSON) UnmarshalJSON(data []byte) error {
|
func (f *FilesystemConfigJSON) UnmarshalJSON(data []byte) error {
|
||||||
t := new(struct {
|
t := new(fsType)
|
||||||
Type string `json:"type"`
|
|
||||||
})
|
|
||||||
if err := json.Unmarshal(data, &t); err != nil {
|
if err := json.Unmarshal(data, &t); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -28,17 +28,11 @@ func TestFilesystemConfigJSON(t *testing.T) {
|
|||||||
`{"type":"cat","meow":true}`, `{"fs":{"type":"cat","meow":true},"magic":3236757504}`},
|
`{"type":"cat","meow":true}`, `{"fs":{"type":"cat","meow":true},"magic":3236757504}`},
|
||||||
|
|
||||||
{"bad impl bind", hst.FilesystemConfigJSON{FilesystemConfig: stubFS{"bind"}},
|
{"bad impl bind", hst.FilesystemConfigJSON{FilesystemConfig: stubFS{"bind"}},
|
||||||
hst.FSImplError{
|
hst.FSImplError{Value: stubFS{"bind"}},
|
||||||
Type: "bind",
|
|
||||||
Value: stubFS{"bind"},
|
|
||||||
},
|
|
||||||
"\x00", "\x00"},
|
"\x00", "\x00"},
|
||||||
|
|
||||||
{"bad impl ephemeral", hst.FilesystemConfigJSON{FilesystemConfig: stubFS{"ephemeral"}},
|
{"bad impl ephemeral", hst.FilesystemConfigJSON{FilesystemConfig: stubFS{"ephemeral"}},
|
||||||
hst.FSImplError{
|
hst.FSImplError{Value: stubFS{"ephemeral"}},
|
||||||
Type: "ephemeral",
|
|
||||||
Value: stubFS{"ephemeral"},
|
|
||||||
},
|
|
||||||
"\x00", "\x00"},
|
"\x00", "\x00"},
|
||||||
|
|
||||||
{"bind", hst.FilesystemConfigJSON{
|
{"bind", hst.FilesystemConfigJSON{
|
||||||
@ -66,12 +60,18 @@ func TestFilesystemConfigJSON(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
t.Run("marshal", func(t *testing.T) {
|
t.Run("marshal", func(t *testing.T) {
|
||||||
|
wantErr := tc.wantErr
|
||||||
|
if errors.As(wantErr, new(hst.FSTypeError)) {
|
||||||
|
// for unsupported implementation tc
|
||||||
|
wantErr = hst.FSImplError{Value: stubFS{"cat"}}
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
d, err := json.Marshal(&tc.want)
|
d, err := json.Marshal(&tc.want)
|
||||||
if !errors.Is(err, tc.wantErr) {
|
if !errors.Is(err, wantErr) {
|
||||||
t.Errorf("Marshal: error = %v, want %v", err, tc.wantErr)
|
t.Errorf("Marshal: error = %v, want %v", err, wantErr)
|
||||||
}
|
}
|
||||||
if tc.wantErr != nil {
|
if wantErr != nil {
|
||||||
goto checkSMarshal
|
goto checkSMarshal
|
||||||
}
|
}
|
||||||
if string(d) != tc.data {
|
if string(d) != tc.data {
|
||||||
@ -82,10 +82,10 @@ func TestFilesystemConfigJSON(t *testing.T) {
|
|||||||
checkSMarshal:
|
checkSMarshal:
|
||||||
{
|
{
|
||||||
d, err := json.Marshal(&sCheck{tc.want, syscall.MS_MGC_VAL})
|
d, err := json.Marshal(&sCheck{tc.want, syscall.MS_MGC_VAL})
|
||||||
if !errors.Is(err, tc.wantErr) {
|
if !errors.Is(err, wantErr) {
|
||||||
t.Errorf("Marshal: error = %v, want %v", err, tc.wantErr)
|
t.Errorf("Marshal: error = %v, want %v", err, wantErr)
|
||||||
}
|
}
|
||||||
if tc.wantErr != nil {
|
if wantErr != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if string(d) != tc.sData {
|
if string(d) != tc.sData {
|
||||||
@ -170,15 +170,15 @@ func TestFSErrors(t *testing.T) {
|
|||||||
val hst.FilesystemConfig
|
val hst.FilesystemConfig
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"nil", nil, "implementation nil is not cat"},
|
{"nil", nil, "implementation nil not supported"},
|
||||||
{"stub", stubFS{"cat"}, "implementation stubFS is not cat"},
|
{"stub", stubFS{"cat"}, "implementation stubFS not supported"},
|
||||||
{"*stub", &stubFS{"cat"}, "implementation *stubFS is not cat"},
|
{"*stub", &stubFS{"cat"}, "implementation *stubFS not supported"},
|
||||||
{"(*stub)(nil)", (*stubFS)(nil), "implementation *stubFS is not cat"},
|
{"(*stub)(nil)", (*stubFS)(nil), "implementation *stubFS not supported"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
err := hst.FSImplError{Type: "cat", Value: tc.val}
|
err := hst.FSImplError{Value: tc.val}
|
||||||
if got := err.Error(); got != tc.want {
|
if got := err.Error(); got != tc.want {
|
||||||
t.Errorf("Error: %q, want %q", got, tc.want)
|
t.Errorf("Error: %q, want %q", got, tc.want)
|
||||||
}
|
}
|
||||||
@ -191,7 +191,6 @@ type stubFS struct {
|
|||||||
typeName string
|
typeName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s stubFS) Type() string { return s.typeName }
|
|
||||||
func (s stubFS) Valid() bool { return false }
|
func (s stubFS) Valid() bool { return false }
|
||||||
func (s stubFS) Target() *container.Absolute { panic("unreachable") }
|
func (s stubFS) Target() *container.Absolute { panic("unreachable") }
|
||||||
func (s stubFS) Host() []*container.Absolute { panic("unreachable") }
|
func (s stubFS) Host() []*container.Absolute { panic("unreachable") }
|
||||||
@ -213,15 +212,9 @@ type fsTestCase struct {
|
|||||||
str string
|
str string
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFs(t *testing.T, fstype string, testCases []fsTestCase) {
|
func checkFs(t *testing.T, testCases []fsTestCase) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
t.Run("type", func(t *testing.T) {
|
|
||||||
if got := tc.fs.Type(); got != fstype {
|
|
||||||
t.Errorf("Type: %q, want %q", got, fstype)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("valid", func(t *testing.T) {
|
t.Run("valid", func(t *testing.T) {
|
||||||
if got := tc.fs.Valid(); got != tc.valid {
|
if got := tc.fs.Valid(); got != tc.valid {
|
||||||
t.Errorf("Valid: %v, want %v", got, tc.valid)
|
t.Errorf("Valid: %v, want %v", got, tc.valid)
|
||||||
|
@ -26,8 +26,7 @@ type FSBind struct {
|
|||||||
Optional bool `json:"optional,omitempty"`
|
Optional bool `json:"optional,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *FSBind) Type() string { return FilesystemBind }
|
func (b *FSBind) Valid() bool { return b != nil && b.Src != nil }
|
||||||
func (b *FSBind) Valid() bool { return b != nil && b.Src != nil }
|
|
||||||
|
|
||||||
func (b *FSBind) Target() *container.Absolute {
|
func (b *FSBind) Target() *container.Absolute {
|
||||||
if !b.Valid() {
|
if !b.Valid() {
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFSBind(t *testing.T) {
|
func TestFSBind(t *testing.T) {
|
||||||
checkFs(t, "bind", []fsTestCase{
|
checkFs(t, []fsTestCase{
|
||||||
{"nil", (*hst.FSBind)(nil), false, nil, nil, nil, "<invalid>"},
|
{"nil", (*hst.FSBind)(nil), false, nil, nil, nil, "<invalid>"},
|
||||||
|
|
||||||
{"full", &hst.FSBind{
|
{"full", &hst.FSBind{
|
||||||
|
@ -25,8 +25,7 @@ type FSEphemeral struct {
|
|||||||
Perm os.FileMode `json:"perm,omitempty"`
|
Perm os.FileMode `json:"perm,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *FSEphemeral) Type() string { return FilesystemEphemeral }
|
func (e *FSEphemeral) Valid() bool { return e != nil && e.Dst != nil }
|
||||||
func (e *FSEphemeral) Valid() bool { return e != nil && e.Dst != nil }
|
|
||||||
|
|
||||||
func (e *FSEphemeral) Target() *container.Absolute {
|
func (e *FSEphemeral) Target() *container.Absolute {
|
||||||
if !e.Valid() {
|
if !e.Valid() {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFSEphemeral(t *testing.T) {
|
func TestFSEphemeral(t *testing.T) {
|
||||||
checkFs(t, "ephemeral", []fsTestCase{
|
checkFs(t, []fsTestCase{
|
||||||
{"nil", (*hst.FSEphemeral)(nil), false, nil, nil, nil, "<invalid>"},
|
{"nil", (*hst.FSEphemeral)(nil), false, nil, nil, nil, "<invalid>"},
|
||||||
|
|
||||||
{"full", &hst.FSEphemeral{
|
{"full", &hst.FSEphemeral{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user