diff --git a/NOTABLE_CHANGES_FROM_SPRIG_NOTES.md b/NOTABLE_CHANGES_FROM_SPRIG_NOTES.md deleted file mode 100644 index 3102154..0000000 --- a/NOTABLE_CHANGES_FROM_SPRIG_NOTES.md +++ /dev/null @@ -1,72 +0,0 @@ -# Migration notes - -## DeepCopy - -```go -if err != nil { - panic("deepCopy error: " + err.Error()) -} -``` -changed to -```go -if err != nil { - return nil -} -``` - -## MustDeepCopy -In sprig MustDeepCopy accept nil as input and cause internal panic. In sprout MustDeepCopy return nil if input is nil. - - -## all rand functions -In sprig all rand functions cause internal panic if the length is equal to 0. In sprout all rand functions return empty string if the length is equal to 0. - - - -## ToRawJson -in sprig code panic -```go -func toRawJson(v interface{}) string { - output, err := mustToRawJson(v) - if err != nil { - panic(err) - } - return string(output) -} -``` - -in sprout code follow the same pattern as other functions - -```go -func (fh *FunctionHandler) ToRawJson(v any) string { - output, _ := fh.MustToRawJson(v) - return output -} -``` - -### DateAgo - -In sprig this function dont support int32 and *time.Time and cause result to "0s" -In sprout this function support int32 and *time.Time and return the correct result - -### DateRound -In sprig When we pass a negative value, it will return the correct duration but in positive value. -In sprout When we pass a negative value, it will return the correct duration with in negative value. - -### Append, Prepend, Concat, Chunk, Uniq, Compact, Slice, Without, Rest, Initial, Reverse -In sprig all these functions cause internal panic. -In sprout all these functions return empty slice when an error occurs. - -### First, Last -In sprig all these functions cause internal panic. -In sprout all these functions return nil when an error occurs. - -### MustAppend, MustPrepend, MustConcat, MustChunk, MustUniq, MustCompact, MustSlice, MustWithout, MustRest, MustInitial, MustReverse -In sprig all these functions cause segfault when lsit are nil. -In sprout all these functions return nil and an error when an error occurs. - -### Has, Dig -In sprig this function cause internal panic. -In sprout this function return false when an error occurs. diff --git a/SPRIG_TO_SPROUT_CHANGES_NOTES.md b/SPRIG_TO_SPROUT_CHANGES_NOTES.md new file mode 100644 index 0000000..de8b2f4 --- /dev/null +++ b/SPRIG_TO_SPROUT_CHANGES_NOTES.md @@ -0,0 +1,82 @@ +# Migration Notes for Sprout Library +This document outlines the key differences and migration changes between the +Sprig and Sprout libraries. The changes are designed to enhance stability and +usability in the Sprout library. + +This document will help contributors and maintainers understand the changes made +between the fork date and version 1.0.0 of the Sprout library. + +It will be updated to reflect changes in future versions of the library. + +This document will also assist in creating the migration guide when version 1.0 is ready. + + +## Error Handling Enhancements +### General Error Handling +In Sprig, errors within certain functions cause a panic. +In contrast, Sprout opts for returning nil or an empty value, improving safety +and predictability. + +**Old Behavior (Sprig)**: Triggers a panic on error +```go +if err != nil { + panic("deepCopy error: " + err.Error()) +} +``` + +**New Behavior (Sprout)**: Returns nil or an empty value on error +```go +if err != nil { + return nil, err +} +``` + +Methods that previously caused a panic in Sprig : +- DeepCopy +- MustDeepCopy +- ToRawJson +- Append +- Prepend +- Concat +- Chunk +- Uniq +- Compact +- Slice +- Without +- Rest +- Initial +- Reverse +- First +- Last +- Has +- Dig +- RandAlphaNumeric +- RandAlpha +- RandAscii +- RandNumeric +- RandBytes + +## Function-Specific Changes + +### MustDeepCopy + +- **Sprig**: Accepts `nil` input, causing an internal panic. +- **Sprout**: Returns `nil` if input is `nil`, avoiding panic. + +## Rand Functions + +- **Sprig**: Causes an internal panic if the length parameter is zero. +- **Sprout**: Returns an empty string if the length is zero, ensuring stability. + +## DateAgo + +- **Sprig**: Does not support int32 and *time.Time; returns "0s". +- **Sprout**: Supports int32 and *time.Time and returns the correct duration. + +## DateRound +- **Sprig**: Returns a corrected duration in positive form, even for negative inputs. +- **Sprout**: Accurately returns the duration, preserving the sign of the input. + +## Base32Decode / Base64Decode +- **Sprig**: Decoding functions return the error string when the input is not a valid base64 encoded string. +- **Sprout**: Decoding functions return an empty string if the input is not a valid base64 encoded string, simplifying error handling. diff --git a/encoding_functions.go b/encoding_functions.go index 6dc0e7f..1e95372 100644 --- a/encoding_functions.go +++ b/encoding_functions.go @@ -42,7 +42,7 @@ func (fh *FunctionHandler) Base64Encode(s string) string { func (fh *FunctionHandler) Base64Decode(s string) string { bytes, err := base64.StdEncoding.DecodeString(s) if err != nil { - return err.Error() + return "" } return string(bytes) } @@ -81,7 +81,7 @@ func (fh *FunctionHandler) Base32Encode(s string) string { func (fh *FunctionHandler) Base32Decode(s string) string { bytes, err := base32.StdEncoding.DecodeString(s) if err != nil { - return err.Error() + return "" } return string(bytes) }