mirror of
https://github.com/IBM/fp-go.git
synced 2026-03-08 13:29:18 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66b2f57d73 | ||
|
|
92eb2a50a2 |
@@ -4,7 +4,7 @@ go 1.24
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/urfave/cli/v3 v3.6.2
|
||||
github.com/urfave/cli/v3 v3.7.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -6,6 +6,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
|
||||
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
github.com/urfave/cli/v3 v3.7.0 h1:AGSnbUyjtLiM+WJUb4dzXKldl/gL+F8OwmRDtVr6g2U=
|
||||
github.com/urfave/cli/v3 v3.7.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -22,15 +22,19 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validate"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
"github.com/IBM/fp-go/v2/optics/prism"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// validateFromParser creates a validation function from a parser that may fail.
|
||||
@@ -338,3 +342,128 @@ func Int64FromString() Type[int64, string, string] {
|
||||
prism.ParseInt64().ReverseGet,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeJSON[T any](dec json.Unmarshaler) ReaderResult[[]byte, T] {
|
||||
return func(b []byte) Result[T] {
|
||||
var t T
|
||||
err := dec.UnmarshalJSON(b)
|
||||
return result.TryCatchError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeText[T any](dec encoding.TextUnmarshaler) ReaderResult[[]byte, T] {
|
||||
return func(b []byte) Result[T] {
|
||||
var t T
|
||||
err := dec.UnmarshalText(b)
|
||||
return result.TryCatchError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalText creates a bidirectional codec for types that implement encoding.TextMarshaler
|
||||
// and encoding.TextUnmarshaler. This codec handles binary text serialization formats.
|
||||
//
|
||||
// The codec:
|
||||
// - Decodes: Calls dec.UnmarshalText(b) to deserialize []byte into the target type T
|
||||
// - Encodes: Calls enc.MarshalText() to serialize the value to []byte
|
||||
// - Validates: Returns a failure if UnmarshalText returns an error
|
||||
//
|
||||
// Note: The enc and dec parameters are external marshaler/unmarshaler instances. The
|
||||
// decoded value is the zero value of T after UnmarshalText has been called on dec
|
||||
// (the caller is responsible for ensuring dec holds the decoded state).
|
||||
//
|
||||
// Type Parameters:
|
||||
// - T: The Go type to encode/decode
|
||||
//
|
||||
// Parameters:
|
||||
// - enc: An encoding.TextMarshaler used for encoding values to []byte
|
||||
// - dec: An encoding.TextUnmarshaler used for decoding []byte to the target type
|
||||
//
|
||||
// Returns:
|
||||
// - A Type[T, []byte, []byte] codec that handles text marshaling/unmarshaling
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type MyType struct{ Value string }
|
||||
//
|
||||
// var instance MyType
|
||||
// codec := MarshalText[MyType](instance, &instance)
|
||||
//
|
||||
// // Decode bytes to MyType
|
||||
// result := codec.Decode([]byte(`some text`))
|
||||
//
|
||||
// // Encode MyType to bytes
|
||||
// encoded := codec.Encode(instance)
|
||||
func MarshalText[T any](
|
||||
enc encoding.TextMarshaler,
|
||||
dec encoding.TextUnmarshaler,
|
||||
) Type[T, []byte, []byte] {
|
||||
return MakeType(
|
||||
"UnmarshalText",
|
||||
Is[T](),
|
||||
F.Pipe2(
|
||||
dec,
|
||||
decodeText[T],
|
||||
validate.FromReaderResult,
|
||||
),
|
||||
func(t T) []byte {
|
||||
b, _ := enc.MarshalText()
|
||||
return b
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// MarshalJSON creates a bidirectional codec for types that implement encoding/json's
|
||||
// json.Marshaler and json.Unmarshaler interfaces. This codec handles JSON serialization.
|
||||
//
|
||||
// The codec:
|
||||
// - Decodes: Calls dec.UnmarshalJSON(b) to deserialize []byte JSON into the target type T
|
||||
// - Encodes: Calls enc.MarshalJSON() to serialize the value to []byte JSON
|
||||
// - Validates: Returns a failure if UnmarshalJSON returns an error
|
||||
//
|
||||
// Note: The enc and dec parameters are external marshaler/unmarshaler instances. The
|
||||
// decoded value is the zero value of T after UnmarshalJSON has been called on dec
|
||||
// (the caller is responsible for ensuring dec holds the decoded state).
|
||||
//
|
||||
// Type Parameters:
|
||||
// - T: The Go type to encode/decode
|
||||
//
|
||||
// Parameters:
|
||||
// - enc: A json.Marshaler used for encoding values to JSON []byte
|
||||
// - dec: A json.Unmarshaler used for decoding JSON []byte to the target type
|
||||
//
|
||||
// Returns:
|
||||
// - A Type[T, []byte, []byte] codec that handles JSON marshaling/unmarshaling
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type MyData struct {
|
||||
// Name string `json:"name"`
|
||||
// Value int `json:"value"`
|
||||
// }
|
||||
//
|
||||
// var instance MyData
|
||||
// codec := MarshalJSON[MyData](&instance, &instance)
|
||||
//
|
||||
// // Decode JSON bytes to MyData
|
||||
// result := codec.Decode([]byte(`{"name":"test","value":42}`))
|
||||
//
|
||||
// // Encode MyData to JSON bytes
|
||||
// encoded := codec.Encode(instance)
|
||||
func MarshalJSON[T any](
|
||||
enc json.Marshaler,
|
||||
dec json.Unmarshaler,
|
||||
) Type[T, []byte, []byte] {
|
||||
return MakeType(
|
||||
"UnmarshalJSON",
|
||||
Is[T](),
|
||||
F.Pipe2(
|
||||
dec,
|
||||
decodeJSON[T],
|
||||
validate.FromReaderResult,
|
||||
),
|
||||
func(t T) []byte {
|
||||
b, _ := enc.MarshalJSON()
|
||||
return b
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user