initial commits
parent
d40fa44b46
commit
bb64ec8664
|
@ -1,20 +0,0 @@
|
|||
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)
|
||||
}
|
|
@ -4,6 +4,8 @@ import (
|
|||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-erorr"
|
||||
|
||||
"sourcecode.social/reiver/go-rfc8259/errors"
|
||||
)
|
||||
|
||||
// Parse tries to parse a JSON boolean literal — i.e., either 'false' or 'true'.
|
||||
|
@ -17,7 +19,7 @@ import (
|
|||
// // ...
|
||||
//
|
||||
// var value rfc8259boolean.Boolean
|
||||
// err := rfc8259.ParseTrue(rs, &value)
|
||||
// err := rfc8259.Parse(rs, &value)
|
||||
//
|
||||
// if nil != err {
|
||||
// return err
|
||||
|
@ -26,10 +28,10 @@ import (
|
|||
// fmt.Printf("value = %#v\n", value)
|
||||
func Parse(runescanner io.RuneScanner, dst *Boolean) error {
|
||||
if nil == runescanner {
|
||||
return errNilRuneScanner
|
||||
return rfc8259errors.ErrNilRuneScanner
|
||||
}
|
||||
if nil == dst {
|
||||
return errNilDestination
|
||||
return rfc8259errors.ErrNilDestination
|
||||
}
|
||||
|
||||
var r rune
|
||||
|
@ -39,26 +41,26 @@ func Parse(runescanner io.RuneScanner, dst *Boolean) error {
|
|||
r, _, err = runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if err := runescanner.UnreadRune(); nil != err {
|
||||
return errProblemUnreadingRune(err, r)
|
||||
return rfc8259errors.ErrProblemUnreadingRune(err, r)
|
||||
}
|
||||
}
|
||||
|
||||
switch r {
|
||||
case 'f':
|
||||
if err := ParseFalse(runescanner); nil != err {
|
||||
if err := parseFalse(runescanner); nil != err {
|
||||
return err
|
||||
}
|
||||
*dst = False()
|
||||
return nil
|
||||
case 't':
|
||||
if err := ParseTrue(runescanner); nil != err {
|
||||
if err := parseTrue(runescanner); nil != err {
|
||||
return err
|
||||
}
|
||||
*dst = True()
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-erorr"
|
||||
|
||||
"sourcecode.social/reiver/go-rfc8259/errors"
|
||||
)
|
||||
|
||||
func errProblemParsingBooleanFalse(expected rune, actual rune) error {
|
||||
|
@ -16,32 +18,32 @@ func errProblemParsingBooleanFalse(expected rune, actual rune) error {
|
|||
//
|
||||
// Example usage:
|
||||
//
|
||||
// var rr io.RuneReader
|
||||
// var runescanner io.RuneScanner
|
||||
//
|
||||
// // ...
|
||||
//
|
||||
// err := rfc8259.ParseFalse(rr)
|
||||
// err := rfc8259.ParseFalse(runescanner)
|
||||
//
|
||||
// if nil != err {
|
||||
// fmt.Printf("We did NOT have a 'false', but instead got the error: %s\n", err)
|
||||
// } else {
|
||||
// fmt.Print("We had a 'false'\n")
|
||||
// }
|
||||
func ParseFalse(rr io.RuneReader) error {
|
||||
if nil == rr {
|
||||
return errNilRuneReader
|
||||
func parseFalse(runescanner io.RuneScanner) error {
|
||||
if nil == runescanner {
|
||||
return rfc8259errors.ErrNilRuneScanner
|
||||
}
|
||||
|
||||
{
|
||||
const expected rune = 'f'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -52,13 +54,13 @@ func ParseFalse(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'a'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -69,13 +71,13 @@ func ParseFalse(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'l'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -86,13 +88,13 @@ func ParseFalse(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 's'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -103,13 +105,13 @@ func ParseFalse(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'e'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package rfc8259boolean_test
|
||||
package rfc8259boolean
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -7,8 +7,6 @@ import (
|
|||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-utf8"
|
||||
|
||||
"sourcecode.social/reiver/go-rfc8259/boolean"
|
||||
)
|
||||
|
||||
func TestParseFalse_success(t *testing.T) {
|
||||
|
@ -16,9 +14,9 @@ func TestParseFalse_success(t *testing.T) {
|
|||
var p []byte = []byte("false")
|
||||
|
||||
var reader io.Reader = bytes.NewReader(p)
|
||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
||||
|
||||
err := rfc8259boolean.ParseFalse(runereader)
|
||||
err := parseFalse(runescanner)
|
||||
|
||||
if nil != err {
|
||||
t.Errorf("Did not expect an error but actually got one.")
|
||||
|
@ -223,9 +221,9 @@ func TestParseFalse_failure(t *testing.T) {
|
|||
for testNumber, test := range tests {
|
||||
|
||||
var reader io.Reader = bytes.NewReader(test.Value)
|
||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
||||
|
||||
err := rfc8259boolean.ParseFalse(runereader)
|
||||
err := parseFalse(runescanner)
|
||||
|
||||
if nil == err {
|
||||
t.Errorf("Expected an error but did not actually get one.")
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-erorr"
|
||||
|
||||
"sourcecode.social/reiver/go-rfc8259/errors"
|
||||
)
|
||||
|
||||
func errProblemParsingBooleanTrue(expected rune, actual rune) error {
|
||||
|
@ -16,32 +18,32 @@ func errProblemParsingBooleanTrue(expected rune, actual rune) error {
|
|||
//
|
||||
// Example usage:
|
||||
//
|
||||
// var rr io.RuneReader
|
||||
// var runescanner io.RuneScanner
|
||||
//
|
||||
// // ...
|
||||
//
|
||||
// err := rfc8259.ParseTrue(rr)
|
||||
// err := rfc8259.ParseTrue(runescanner)
|
||||
//
|
||||
// if nil != err {
|
||||
// fmt.Printf("We did NOT have a 'true', but instead got the error: %s\n", err)
|
||||
// } else {
|
||||
// fmt.Print("We had a 'true'\n")
|
||||
// }
|
||||
func ParseTrue(rr io.RuneReader) error {
|
||||
if nil == rr {
|
||||
return errNilRuneReader
|
||||
func parseTrue(runescanner io.RuneScanner) error {
|
||||
if nil == runescanner {
|
||||
return rfc8259errors.ErrNilRuneScanner
|
||||
}
|
||||
|
||||
{
|
||||
const expected rune = 't'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -52,13 +54,13 @@ func ParseTrue(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'r'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -69,13 +71,13 @@ func ParseTrue(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'u'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
@ -86,13 +88,13 @@ func ParseTrue(rr io.RuneReader) error {
|
|||
{
|
||||
const expected rune = 'e'
|
||||
|
||||
r, _, err := rr.ReadRune()
|
||||
r, _, err := runescanner.ReadRune()
|
||||
if nil != err {
|
||||
if io.EOF == err {
|
||||
return errUnexpectedEndOfFile
|
||||
return rfc8259errors.ErrUnexpectedEndOfFile
|
||||
}
|
||||
|
||||
return errProblemReadingRune(err)
|
||||
return rfc8259errors.ErrProblemReadingRune(err)
|
||||
}
|
||||
|
||||
if expected != r {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package rfc8259boolean_test
|
||||
package rfc8259boolean
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -7,8 +7,6 @@ import (
|
|||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-utf8"
|
||||
|
||||
"sourcecode.social/reiver/go-rfc8259/boolean"
|
||||
)
|
||||
|
||||
func TestParseTrue_success(t *testing.T) {
|
||||
|
@ -16,9 +14,9 @@ func TestParseTrue_success(t *testing.T) {
|
|||
var p []byte = []byte("true")
|
||||
|
||||
var reader io.Reader = bytes.NewReader(p)
|
||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
||||
|
||||
err := rfc8259boolean.ParseTrue(runereader)
|
||||
err := parseTrue(runescanner)
|
||||
|
||||
if nil != err {
|
||||
t.Errorf("Did not expect an error but actually got one.")
|
||||
|
@ -215,9 +213,9 @@ func TestParseTrue_failure(t *testing.T) {
|
|||
for testNumber, test := range tests {
|
||||
|
||||
var reader io.Reader = bytes.NewReader(test.Value)
|
||||
var runereader io.RuneReader = utf8.NewRuneReader(reader)
|
||||
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
|
||||
|
||||
err := rfc8259boolean.ParseTrue(runereader)
|
||||
err := parseTrue(runescanner)
|
||||
|
||||
if nil == err {
|
||||
t.Errorf("Expected an error but did not actually get one.")
|
||||
|
|
Loading…
Reference in New Issue