initial commits
parent
ce4186c27e
commit
4e621a6f2d
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259
|
package rfc8259boolean
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
@ -6,7 +6,7 @@ import (
|
||||||
"sourcecode.social/reiver/go-erorr"
|
"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 succeeds, then it return nil, and sets ‘dst’ to the parsed value.
|
||||||
// If it failed, it returns an error.
|
// If it failed, it returns an error.
|
||||||
//
|
//
|
||||||
|
@ -24,7 +24,7 @@ import (
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// fmt.Printf("value = %t\n", value)
|
// 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 {
|
if nil == runescanner {
|
||||||
return errNilRuneScanner
|
return errNilRuneScanner
|
||||||
}
|
}
|
||||||
|
@ -55,13 +55,13 @@ func ParseBoolean(runescanner io.RuneScanner, dst *bool) error {
|
||||||
if err := ParseFalse(runescanner); nil != err {
|
if err := ParseFalse(runescanner); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*dst = false
|
*dst = False()
|
||||||
return nil
|
return nil
|
||||||
case 't':
|
case 't':
|
||||||
if err := ParseTrue(runescanner); nil != err {
|
if err := ParseTrue(runescanner); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*dst = true
|
*dst = True()
|
||||||
return nil
|
return nil
|
||||||
default:
|
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)
|
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)
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259_test
|
package rfc8259boolean_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -8,22 +8,22 @@ import (
|
||||||
|
|
||||||
"sourcecode.social/reiver/go-utf8"
|
"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{
|
tests := []struct{
|
||||||
Value []byte
|
Value []byte
|
||||||
Expected bool
|
Expected rfc8259boolean.Boolean
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Value: []byte("false"),
|
Value: []byte("false"),
|
||||||
Expected: false,
|
Expected: rfc8259boolean.False(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Value : []byte("true"),
|
Value : []byte("true"),
|
||||||
Expected: true,
|
Expected: rfc8259boolean.True(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ func TestParseBoolean_success(t *testing.T) {
|
||||||
var reader io.Reader = bytes.NewReader(test.Value)
|
var reader io.Reader = bytes.NewReader(test.Value)
|
||||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
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 {
|
if nil != err {
|
||||||
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
|
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 {
|
if expected != actual {
|
||||||
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
|
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
|
||||||
t.Logf("EXPECTED: %t", expected)
|
t.Logf("EXPECTED: %#v", expected)
|
||||||
t.Logf("ACTUAL: %t", actual)
|
t.Logf("ACTUAL: %#v", actual)
|
||||||
t.Logf("VALUE: %#v", test.Value)
|
t.Logf("VALUE: %#v", test.Value)
|
||||||
t.Logf("VALUE: %q", test.Value)
|
t.Logf("VALUE: %q", test.Value)
|
||||||
continue
|
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{
|
tests := []struct{
|
||||||
Value []byte
|
Value []byte
|
||||||
|
@ -291,17 +291,31 @@ func TestParseBoolean_failure(t *testing.T) {
|
||||||
var reader io.Reader = bytes.NewReader(test.Value)
|
var reader io.Reader = bytes.NewReader(test.Value)
|
||||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
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 {
|
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: %#v", test.Value)
|
||||||
t.Logf("VALUE: %q", test.Value)
|
t.Logf("VALUE: %q", test.Value)
|
||||||
continue
|
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()
|
actual := err.Error()
|
||||||
expected := test.Expected
|
expected := test.Expected
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259
|
package rfc8259boolean
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259_test
|
package rfc8259boolean_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
"sourcecode.social/reiver/go-utf8"
|
"sourcecode.social/reiver/go-utf8"
|
||||||
|
|
||||||
"sourcecode.social/reiver/go-rfc8259"
|
"sourcecode.social/reiver/go-rfc8259/boolean"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseFalse_success(t *testing.T) {
|
func TestParseFalse_success(t *testing.T) {
|
||||||
|
@ -18,7 +18,7 @@ func TestParseFalse_success(t *testing.T) {
|
||||||
var reader io.Reader = bytes.NewReader(p)
|
var reader io.Reader = bytes.NewReader(p)
|
||||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||||
|
|
||||||
err := rfc8259.ParseFalse(runereader)
|
err := rfc8259boolean.ParseFalse(runereader)
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
t.Errorf("Did not expect an error but actually got one.")
|
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 reader io.Reader = bytes.NewReader(test.Value)
|
||||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||||
|
|
||||||
err := rfc8259.ParseFalse(runereader)
|
err := rfc8259boolean.ParseFalse(runereader)
|
||||||
|
|
||||||
if nil == err {
|
if nil == err {
|
||||||
t.Errorf("Expected an error but did not actually get one.")
|
t.Errorf("Expected an error but did not actually get one.")
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259
|
package rfc8259boolean
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
|
@ -1,4 +1,4 @@
|
||||||
package rfc8259_test
|
package rfc8259boolean_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
"sourcecode.social/reiver/go-utf8"
|
"sourcecode.social/reiver/go-utf8"
|
||||||
|
|
||||||
"sourcecode.social/reiver/go-rfc8259"
|
"sourcecode.social/reiver/go-rfc8259/boolean"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseTrue_success(t *testing.T) {
|
func TestParseTrue_success(t *testing.T) {
|
||||||
|
@ -18,7 +18,7 @@ func TestParseTrue_success(t *testing.T) {
|
||||||
var reader io.Reader = bytes.NewReader(p)
|
var reader io.Reader = bytes.NewReader(p)
|
||||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||||
|
|
||||||
err := rfc8259.ParseTrue(runereader)
|
err := rfc8259boolean.ParseTrue(runereader)
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
t.Errorf("Did not expect an error but actually got one.")
|
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 reader io.Reader = bytes.NewReader(test.Value)
|
||||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||||
|
|
||||||
err := rfc8259.ParseTrue(runereader)
|
err := rfc8259boolean.ParseTrue(runereader)
|
||||||
|
|
||||||
if nil == err {
|
if nil == err {
|
||||||
t.Errorf("Expected an error but did not actually get one.")
|
t.Errorf("Expected an error but did not actually get one.")
|
Loading…
Reference in New Issue