From 4e621a6f2d76f2eaba9e014d20be550da62ef650 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 3 Nov 2023 16:21:12 -0700 Subject: [PATCH] initial commits --- boolean/boolean.go | 44 ++++++++++++++++++ boolean/errors.go | 20 ++++++++ parseboolean.go => boolean/parse.go | 10 ++-- parseboolean_test.go => boolean/parse_test.go | 46 ++++++++++++------- parsefalse.go => boolean/parsefalse.go | 2 +- .../parsefalse_test.go | 8 ++-- parsetrue.go => boolean/parsetrue.go | 2 +- .../parsetrue_test.go | 8 ++-- 8 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 boolean/boolean.go create mode 100644 boolean/errors.go rename parseboolean.go => boolean/parse.go (85%) rename parseboolean_test.go => boolean/parse_test.go (90%) rename parsefalse.go => boolean/parsefalse.go (98%) rename parsefalse_test.go => boolean/parsefalse_test.go (98%) rename parsetrue.go => boolean/parsetrue.go (98%) rename parsetrue_test.go => boolean/parsetrue_test.go (97%) diff --git a/boolean/boolean.go b/boolean/boolean.go new file mode 100644 index 0000000..b3ab294 --- /dev/null +++ b/boolean/boolean.go @@ -0,0 +1,44 @@ +package rfc8259boolean + +import ( + "fmt" + + "sourcecode.social/reiver/go-opt" +) + +type Boolean struct { + opt.Optional[bool] +} + +func Nothing() Boolean { + return Boolean{opt.Nothing[bool]()} +} + +func False() Boolean { + return Something(false) +} + +func Something(value bool) Boolean { + return Boolean{opt.Something(value)} +} + +func True() Boolean { + return Something(true) +} + +func (receiver Boolean) GoString() string { + switch receiver { + case Nothing(): + return "rfc8259boolean.Nothing()" + case False(): + return "rfc8259boolean.False()" + case True(): + return "rfc8259boolean.True()" + default: + value, found := receiver.Get() + if !found { + return fmt.Sprintf("--INTERNAL-ERROR--") + } + return fmt.Sprintf("rfc8259boolean.Something(%#v)", value) + } +} diff --git a/boolean/errors.go b/boolean/errors.go new file mode 100644 index 0000000..a91f696 --- /dev/null +++ b/boolean/errors.go @@ -0,0 +1,20 @@ +package rfc8259boolean + +import ( + "sourcecode.social/reiver/go-erorr" +) + +const ( + errNilDestination = erorr.Error("rfc8259: nil destination") + errNilRuneReader = erorr.Error("rfc8259: nil rune-reader") + errNilRuneScanner = erorr.Error("rfc8259: nil rune-scanner") + errUnexpectedEndOfFile = erorr.Error("rfc8259: unexpected end-of-file") +) + +func errProblemReadingRune(err error) error { + return erorr.Errorf("rfc8259: problem with JSON parser — problem reading rune: %w", err) +} + +func errProblemUnreadingRune(err error, r rune) error { + return erorr.Errorf("rfc8259: internal-error with JSON parser — problem unreading rune %q (%U): %w", r, r, err) +} diff --git a/parseboolean.go b/boolean/parse.go similarity index 85% rename from parseboolean.go rename to boolean/parse.go index 9badea8..229238e 100644 --- a/parseboolean.go +++ b/boolean/parse.go @@ -1,4 +1,4 @@ -package rfc8259 +package rfc8259boolean import ( "io" @@ -6,7 +6,7 @@ import ( "sourcecode.social/reiver/go-erorr" ) -// ParseBoolean tries to parse a JSON boolean literal — i.e., either 'false' or 'true'. +// Parse tries to parse a JSON boolean literal — i.e., either 'false' or 'true'. // If it succeeds, then it return nil, and sets ‘dst’ to the parsed value. // If it failed, it returns an error. // @@ -24,7 +24,7 @@ import ( // } // // fmt.Printf("value = %t\n", value) -func ParseBoolean(runescanner io.RuneScanner, dst *bool) error { +func Parse(runescanner io.RuneScanner, dst *Boolean) error { if nil == runescanner { return errNilRuneScanner } @@ -55,13 +55,13 @@ func ParseBoolean(runescanner io.RuneScanner, dst *bool) error { if err := ParseFalse(runescanner); nil != err { return err } - *dst = false + *dst = False() return nil case 't': if err := ParseTrue(runescanner); nil != err { return err } - *dst = true + *dst = True() return nil default: return erorr.Errorf("rfc8259: problem when trying to parse JSON boolean literal — expected either 'false' or 'true' but first character was %q (%U)", r, r) diff --git a/parseboolean_test.go b/boolean/parse_test.go similarity index 90% rename from parseboolean_test.go rename to boolean/parse_test.go index 699d1cc..e4ae658 100644 --- a/parseboolean_test.go +++ b/boolean/parse_test.go @@ -1,4 +1,4 @@ -package rfc8259_test +package rfc8259boolean_test import ( "testing" @@ -8,22 +8,22 @@ import ( "sourcecode.social/reiver/go-utf8" - "sourcecode.social/reiver/go-rfc8259" + "sourcecode.social/reiver/go-rfc8259/boolean" ) -func TestParseBoolean_success(t *testing.T) { +func TestParse_success(t *testing.T) { tests := []struct{ Value []byte - Expected bool + Expected rfc8259boolean.Boolean }{ { - Value: []byte("false"), - Expected: false, + Value: []byte("false"), + Expected: rfc8259boolean.False(), }, { - Value: []byte("true"), - Expected: true, + Value : []byte("true"), + Expected: rfc8259boolean.True(), }, } @@ -32,9 +32,9 @@ func TestParseBoolean_success(t *testing.T) { var reader io.Reader = bytes.NewReader(test.Value) var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - var actual bool + var actual rfc8259boolean.Boolean - err := rfc8259.ParseBoolean(runescanner, &actual) + err := rfc8259boolean.Parse(runescanner, &actual) if nil != err { t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber) @@ -50,8 +50,8 @@ func TestParseBoolean_success(t *testing.T) { if expected != actual { t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) - t.Logf("EXPECTED: %t", expected) - t.Logf("ACTUAL: %t", actual) + t.Logf("EXPECTED: %#v", expected) + t.Logf("ACTUAL: %#v", actual) t.Logf("VALUE: %#v", test.Value) t.Logf("VALUE: %q", test.Value) continue @@ -60,7 +60,7 @@ func TestParseBoolean_success(t *testing.T) { } } -func TestParseBoolean_failure(t *testing.T) { +func TestParse_failure(t *testing.T) { tests := []struct{ Value []byte @@ -291,17 +291,31 @@ func TestParseBoolean_failure(t *testing.T) { var reader io.Reader = bytes.NewReader(test.Value) var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - var actual bool + var actual rfc8259boolean.Boolean - err := rfc8259.ParseBoolean(runescanner, &actual) + err := rfc8259boolean.Parse(runescanner, &actual) if nil == err { - t.Errorf("Expected an error but did not actually get one.") + t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber) t.Logf("VALUE: %#v", test.Value) t.Logf("VALUE: %q", test.Value) continue } + + { + expected := rfc8259boolean.Nothing() + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %#v", expected) + t.Logf("ACTUAL: %#v", actual) + t.Logf("VALUE: %#v", test.Value) + t.Logf("VALUE: %q", test.Value) + continue + } + } + { actual := err.Error() expected := test.Expected diff --git a/parsefalse.go b/boolean/parsefalse.go similarity index 98% rename from parsefalse.go rename to boolean/parsefalse.go index 6ca490d..3415fd6 100644 --- a/parsefalse.go +++ b/boolean/parsefalse.go @@ -1,4 +1,4 @@ -package rfc8259 +package rfc8259boolean import ( "io" diff --git a/parsefalse_test.go b/boolean/parsefalse_test.go similarity index 98% rename from parsefalse_test.go rename to boolean/parsefalse_test.go index d08f601..fe5e8e3 100644 --- a/parsefalse_test.go +++ b/boolean/parsefalse_test.go @@ -1,4 +1,4 @@ -package rfc8259_test +package rfc8259boolean_test import ( "testing" @@ -8,7 +8,7 @@ import ( "sourcecode.social/reiver/go-utf8" - "sourcecode.social/reiver/go-rfc8259" + "sourcecode.social/reiver/go-rfc8259/boolean" ) func TestParseFalse_success(t *testing.T) { @@ -18,7 +18,7 @@ func TestParseFalse_success(t *testing.T) { var reader io.Reader = bytes.NewReader(p) var runereader io.RuneReader = utf8.NewRuneReader(reader) - err := rfc8259.ParseFalse(runereader) + err := rfc8259boolean.ParseFalse(runereader) if nil != err { t.Errorf("Did not expect an error but actually got one.") @@ -225,7 +225,7 @@ func TestParseFalse_failure(t *testing.T) { var reader io.Reader = bytes.NewReader(test.Value) var runereader io.RuneReader = utf8.NewRuneReader(reader) - err := rfc8259.ParseFalse(runereader) + err := rfc8259boolean.ParseFalse(runereader) if nil == err { t.Errorf("Expected an error but did not actually get one.") diff --git a/parsetrue.go b/boolean/parsetrue.go similarity index 98% rename from parsetrue.go rename to boolean/parsetrue.go index 24d9176..0625ffa 100644 --- a/parsetrue.go +++ b/boolean/parsetrue.go @@ -1,4 +1,4 @@ -package rfc8259 +package rfc8259boolean import ( "io" diff --git a/parsetrue_test.go b/boolean/parsetrue_test.go similarity index 97% rename from parsetrue_test.go rename to boolean/parsetrue_test.go index 448c2fd..4d14956 100644 --- a/parsetrue_test.go +++ b/boolean/parsetrue_test.go @@ -1,4 +1,4 @@ -package rfc8259_test +package rfc8259boolean_test import ( "testing" @@ -8,7 +8,7 @@ import ( "sourcecode.social/reiver/go-utf8" - "sourcecode.social/reiver/go-rfc8259" + "sourcecode.social/reiver/go-rfc8259/boolean" ) func TestParseTrue_success(t *testing.T) { @@ -18,7 +18,7 @@ func TestParseTrue_success(t *testing.T) { var reader io.Reader = bytes.NewReader(p) var runereader io.RuneReader = utf8.NewRuneReader(reader) - err := rfc8259.ParseTrue(runereader) + err := rfc8259boolean.ParseTrue(runereader) if nil != err { t.Errorf("Did not expect an error but actually got one.") @@ -217,7 +217,7 @@ func TestParseTrue_failure(t *testing.T) { var reader io.Reader = bytes.NewReader(test.Value) var runereader io.RuneReader = utf8.NewRuneReader(reader) - err := rfc8259.ParseTrue(runereader) + err := rfc8259boolean.ParseTrue(runereader) if nil == err { t.Errorf("Expected an error but did not actually get one.")