| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package types |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"strconv" |
| 12 |
) |
| 13 |
|
| 14 |
// TestDeepStringer is a TestDeep specific interface for objects which |
| 15 |
// know how to stringify themselves. |
| 16 |
type TestDeepStringer interface { |
| 17 |
_TestDeep() |
| 18 |
String() string |
| 19 |
} |
| 20 |
|
| 21 |
// TestDeepStamp is a useful type providing the _TestDeep() method |
| 22 |
// needed to implement [TestDeepStringer] interface. |
| 23 |
type TestDeepStamp struct{} |
| 24 |
|
| 25 |
func (t TestDeepStamp) _TestDeep() {} |
| 26 |
|
| 27 |
// RawString implements [TestDeepStringer] interface. |
| 28 |
type RawString string |
| 29 |
|
| 30 |
func (s RawString) _TestDeep() {} |
| 31 |
|
| 32 |
func (s RawString) String() string { |
| 33 |
return string(s) |
| 34 |
} |
| 35 |
|
| 36 |
// RawInt implements [TestDeepStringer] interface. |
| 37 |
type RawInt int |
| 38 |
|
| 39 |
func (i RawInt) _TestDeep() {} |
| 40 |
|
| 41 |
func (i RawInt) String() string { |
| 42 |
return strconv.Itoa(int(i)) |
| 43 |
} |
| 44 |
|
| 45 |
var _ = []TestDeepStringer{RawString(""), RawInt(0)} |
| 46 |
|
| 47 |
// OperatorNotJSONMarshallableError implements error interface. It |
| 48 |
// is returned by (*td.TestDeep).MarshalJSON() to notice the user an |
| 49 |
// operator cannot be JSON Marshal'ed. |
| 50 |
type OperatorNotJSONMarshallableError string |
| 51 |
|
| 52 |
// Error implements error interface. |
| 53 |
func (e OperatorNotJSONMarshallableError) Error() string { |
| 54 |
return string(e) + " TestDeep operator cannot be json.Marshal'led" |
| 55 |
} |
| 56 |
|
| 57 |
// Operator returns the operator behind this error. |
| 58 |
func (e OperatorNotJSONMarshallableError) Operator() string { |
| 59 |
return string(e) |
| 60 |
} |
| 61 |
|
| 62 |
// AsOperatorNotJSONMarshallableError checks that err is or contains |
| 63 |
// an [OperatorNotJSONMarshallableError] and if yes, returns it and |
| 64 |
// true. |
| 65 |
func AsOperatorNotJSONMarshallableError(err error) (OperatorNotJSONMarshallableError, bool) { |
| 66 |
switch err := err.(type) { |
| 67 |
case OperatorNotJSONMarshallableError: |
| 68 |
return err, true |
| 69 |
|
| 70 |
case *json.MarshalerError: |
| 71 |
if err, ok := err.Err.(OperatorNotJSONMarshallableError); ok { |
| 72 |
return err, true |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return "", false |
| 77 |
} |
| 78 |
|
| 79 |
type RecvKind bool |
| 80 |
|
| 81 |
const ( |
| 82 |
_ RecvKind = (iota & 1) == 0 |
| 83 |
RecvNothing |
| 84 |
RecvClosed |
| 85 |
) |
| 86 |
|
| 87 |
func (r RecvKind) _TestDeep() {} |
| 88 |
|
| 89 |
func (r RecvKind) String() string { |
| 90 |
if r == RecvNothing { |
| 91 |
return "nothing received on channel" |
| 92 |
} |
| 93 |
return "channel is closed" |
| 94 |
} |
| 95 |
|
| 96 |
var _ = []TestDeepStringer{RecvNothing, RecvClosed} |