From 55d10f4d256a39e92749da726585f0d31a3dd0df Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 3 Oct 2023 12:56:17 +0100 Subject: [PATCH] fs: re-implement DumpMode with Bits This almost 100% backwards compatible. The only difference being that in the rc options/get output DumpMode will be output as strings instead of integers. This is a lot more convenient for the user. They still accept integer inputs though so the fallout from this should be minimal. --- fs/dump.go | 95 +++++++++---------------------------------------- fs/dump_test.go | 8 ++--- 2 files changed, 21 insertions(+), 82 deletions(-) diff --git a/fs/dump.go b/fs/dump.go index 40d7d80c6..5837145cc 100644 --- a/fs/dump.go +++ b/fs/dump.go @@ -1,12 +1,7 @@ package fs -import ( - "fmt" - "strings" -) - // DumpFlags describes the Dump options in force -type DumpFlags int +type DumpFlags = Bits[dumpChoices] // DumpFlags definitions const ( @@ -20,80 +15,24 @@ const ( DumpOpenFiles ) -var dumpFlags = []struct { - flag DumpFlags - name string -}{ - {DumpHeaders, "headers"}, - {DumpBodies, "bodies"}, - {DumpRequests, "requests"}, - {DumpResponses, "responses"}, - {DumpAuth, "auth"}, - {DumpFilters, "filters"}, - {DumpGoRoutines, "goroutines"}, - {DumpOpenFiles, "openfiles"}, +type dumpChoices struct{} + +func (dumpChoices) Choices() []BitsChoicesInfo { + return []BitsChoicesInfo{ + {uint64(DumpHeaders), "headers"}, + {uint64(DumpBodies), "bodies"}, + {uint64(DumpRequests), "requests"}, + {uint64(DumpResponses), "responses"}, + {uint64(DumpAuth), "auth"}, + {uint64(DumpFilters), "filters"}, + {uint64(DumpGoRoutines), "goroutines"}, + {uint64(DumpOpenFiles), "openfiles"}, + } } -// DumpFlagsList is a list of dump flags used in the help -var DumpFlagsList string - -func init() { - // calculate the dump flags list - var out []string - for _, info := range dumpFlags { - out = append(out, info.name) - } - DumpFlagsList = strings.Join(out, ",") -} - -// String turns a DumpFlags into a string -func (f DumpFlags) String() string { - var out []string - for _, info := range dumpFlags { - if f&info.flag != 0 { - out = append(out, info.name) - f &^= info.flag - } - } - if f != 0 { - out = append(out, fmt.Sprintf("Unknown-0x%X", int(f))) - } - return strings.Join(out, ",") -} - -// Set a DumpFlags as a comma separated list of flags -func (f *DumpFlags) Set(s string) error { - var flags DumpFlags - parts := strings.Split(s, ",") - for _, part := range parts { - found := false - part = strings.ToLower(strings.TrimSpace(part)) - if part == "" { - continue - } - for _, info := range dumpFlags { - if part == info.name { - found = true - flags |= info.flag - } - } - if !found { - return fmt.Errorf("unknown dump flag %q", part) - } - } - *f = flags - return nil -} - -// Type of the value -func (f DumpFlags) Type() string { +func (dumpChoices) Type() string { return "DumpFlags" } -// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON -func (f *DumpFlags) UnmarshalJSON(in []byte) error { - return UnmarshalJSONFlag(in, f, func(i int64) error { - *f = (DumpFlags)(i) - return nil - }) -} +// DumpFlagsList is a list of dump flags used in the help +var DumpFlagsList = DumpHeaders.Help() diff --git a/fs/dump_test.go b/fs/dump_test.go index d374672f1..b0bba60d0 100644 --- a/fs/dump_test.go +++ b/fs/dump_test.go @@ -33,9 +33,9 @@ func TestDumpFlagsSet(t *testing.T) { {"bodies,headers,auth", DumpBodies | DumpHeaders | DumpAuth, ""}, {"bodies,headers,auth", DumpBodies | DumpHeaders | DumpAuth, ""}, {"headers,bodies,requests,responses,auth,filters", DumpHeaders | DumpBodies | DumpRequests | DumpResponses | DumpAuth | DumpFilters, ""}, - {"headers,bodies,unknown,auth", 0, "unknown dump flag \"unknown\""}, + {"headers,bodies,unknown,auth", 0, "invalid choice \"unknown\""}, } { - f := DumpFlags(-1) + f := DumpFlags(0xffffffffffffffff) initial := f err := f.Set(test.in) if err != nil { @@ -72,12 +72,12 @@ func TestDumpFlagsUnmarshallJSON(t *testing.T) { {`"bodies,headers,auth"`, DumpBodies | DumpHeaders | DumpAuth, ""}, {`"bodies,headers,auth"`, DumpBodies | DumpHeaders | DumpAuth, ""}, {`"headers,bodies,requests,responses,auth,filters"`, DumpHeaders | DumpBodies | DumpRequests | DumpResponses | DumpAuth | DumpFilters, ""}, - {`"headers,bodies,unknown,auth"`, 0, "unknown dump flag \"unknown\""}, + {`"headers,bodies,unknown,auth"`, 0, "invalid choice \"unknown\""}, {`0`, DumpFlags(0), ""}, {strconv.Itoa(int(DumpBodies)), DumpBodies, ""}, {strconv.Itoa(int(DumpBodies | DumpHeaders | DumpAuth)), DumpBodies | DumpHeaders | DumpAuth, ""}, } { - f := DumpFlags(-1) + f := DumpFlags(0xffffffffffffffff) initial := f err := json.Unmarshal([]byte(test.in), &f) if err != nil {