commit 0e111aa2a50b1cde5dbd312b3b456f3be6c1389c
parent bb09b7fd5e4006be24782681c8b01dc29874b164
Author: hhvn <dev@hhvn.uk>
Date: Sun, 18 Feb 2024 17:24:51 +0000
Document method signatures
Diffstat:
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/emarshal.go b/emarshal.go
@@ -7,7 +7,7 @@
// The intended use case (as seen in the example) is for these functions to be
// run by the MarshalJSON/UnmarshalJSON (or equivalent for other encodings)
// methods on the parent type.
-package emarshal
+package emarshal // import "hhvn.uk/go-emarshal"
import (
"fmt"
@@ -23,13 +23,19 @@ var (
ErrBadUnmarshal = errors.New("bad unmarshaller")
)
+// MarshalFunc is a function that can be passed to Marshal().
type MarshalFunc func(any) ([]byte, error)
+// EMarshalMethod documents the signature of an EMarshal... method
+type EMarshalMethod func() (any, error)
+
// Marshal returns an encoded form of in.
//
// The encoded form is made by running all methods with the prefix "EMarshal"
// of in, and then combining these values into a single struct and encoding it
// with fn.
+//
+// The signature of a such a method is documented by EMarshalMethod.
func Marshal(in any, fn MarshalFunc) ([]byte, error) {
t := reflect.TypeOf(in)
tn := t.Name()
@@ -75,14 +81,22 @@ func Marshal(in any, fn MarshalFunc) ([]byte, error) {
return fn(s)
}
+// UnmarshalFunc is a function that can be passed to Unmarshal().
type UnmarshalFunc func([]byte, any) error
+
+// Unpack is the function that is passed to an EUnmarshalMethod.
type Unpack func(any) error
+// EUnmarshalMethod documents the signature of an EUnmarshal... method
+type EUnmarshalMethod func(Unpack) error
+
// Unmarshal decodes a byte slice into the value pointed to by in.
//
// It does this by running all the methods with the prefix "EUnmarshal" of in.
// The methods are passed an anonymous function that will unpack the data into
// a chosen variable using fn.
+//
+// The signature of a such a method is documented by EUnmarshalMethod.
func Unmarshal(in any, b []byte, fn UnmarshalFunc) error {
t := reflect.TypeOf(in)