Skip to content

Commit

Permalink
doc: replace all go template example by doc link (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys authored Dec 5, 2024
1 parent aba61cb commit a3026c2
Show file tree
Hide file tree
Showing 31 changed files with 1,110 additions and 1,043 deletions.
2 changes: 1 addition & 1 deletion docs/roadmap-to-sprout-v1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Aim to minimize memory allocations as much as possible to alleviate the burden o

### :white\_check\_mark: Native Error Handling - <mark style="color:green;">**DONE**</mark>

Follow default go template error handling mechanisms for all functions to ensure that errors are managed gracefully and efficiently.
Follow default Go template error handling mechanisms for all functions to ensure that errors are managed gracefully and efficiently.

{% hint style="success" %}
These features are implemented on v0.6.0, documentation can be found here:
Expand Down
15 changes: 13 additions & 2 deletions registry/_example/_example.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// This package is an example of how to create a new registry for Sprout.
//
// You can use this package as a template to create your own registry and
// replace all instances of `example` with your registry name following the
// conventions. You can see more on [Documentation]
//
// [Documentation]: https://docs.atom.codes/sprout/advanced/how-to-create-a-registry
package example

import (
Expand Down Expand Up @@ -31,11 +38,15 @@ func (or *ExampleRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error
}

func (or *ExampleRegistry) RegisterAliases(aliasMap sprout.FunctionAliasMap) error {
// Register your alias here if you have any or remove this method
// Register your alias here if you have any or remove this method if you don't have any
// You can see more on [Documentation]
// [Documentation]: https://docs.atom.codes/sprout/features/function-aliases
return nil
}

func (or *ExampleRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error {
// Register your notices here if you have any or remove this method
// Register your notices here if you have any or remove this method if you don't have any
// You can see more on [Documentation]
// [Documentation]: https://docs.atom.codes/sprout/features/function-notices
return nil
}
8 changes: 8 additions & 0 deletions registry/_example/functions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package example

// ExampleFunction is a function that does something.
//
// Parameters:
//
// Returns:
//
// For an example of this function in a Go template, refer to [Sprout Documentation: exampleFunction].
//
// [Sprout Documentation: exampleFunction]: https://docs.atom.codes/sprout/registries/example#examplefunction
func (or *ExampleRegistry) ExampleFunction() (string, error) {
// Do something with helper
or.helperFunction()
Expand Down
40 changes: 20 additions & 20 deletions registry/backward/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
// *uint - always returns nil, indicating no value is associated with the failure.
// error - the error object containing the provided message.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: sha256Sum].
//
// {{ "Operation failed" | fail }} // Output: nil, error with "Operation failed"
// [Sprout Documentation: sha256Sum]: https://docs.atom.codes/sprout/registries/backward#fail
func (bcr *BackwardCompatibilityRegistry) Fail(message string) (*uint, error) {
return nil, errors.New(message)
}
Expand All @@ -35,20 +35,20 @@ func (bcr *BackwardCompatibilityRegistry) Fail(message string) (*uint, error) {
//
// Parameters:
//
// v string - the URL string to parse.
// value string - the URL string to parse.
//
// Returns:
//
// map[string]any - a map containing the URL components: "scheme", "host",
// "hostname", "path", "query", "opaque", "fragment", and "userinfo".
// error - an error object if the URL string is invalid.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: urlParse].
//
// {{ "https://example.com/path?query=1#fragment" | urlParse }} // Output: map[fragment:fragment host:example.com hostname:example.com path:path query:query scheme:https]
func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, error) {
// [Sprout Documentation: urlParse]: https://docs.atom.codes/sprout/registries/backward#urlparse
func (bcr *BackwardCompatibilityRegistry) UrlParse(value string) (map[string]any, error) {
dict := map[string]any{}
parsedURL, err := url.Parse(v)
parsedURL, err := url.Parse(value)
if err != nil {
return dict, fmt.Errorf("unable to parse url: %w", err)
}
Expand All @@ -73,27 +73,27 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, er
//
// Parameters:
//
// d map[string]any - a map containing the URL components: "scheme", "host",
// dataMap map[string]any - a map containing the URL components: "scheme", "host",
// "path", "query", "opaque", "fragment", and "userinfo".
//
// Returns:
//
// string - the constructed URL string.
// error - an error object if the URL components are invalid.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: urlJoin].
//
// {{ dict scheme="https" host="example.com" path="/path" query="query=1" opaque="opaque" fragment="fragment" | urlJoin }} // Output: "https://example.com/path?query=1#fragment"
func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, error) {
// [Sprout Documentation: urlJoin]: https://docs.atom.codes/sprout/registries/backward#urljoin
func (bcr *BackwardCompatibilityRegistry) UrlJoin(dataMap map[string]any) (string, error) {
resURL := url.URL{
Scheme: bcr.get(d, "scheme").(string),
Host: bcr.get(d, "host").(string),
Path: bcr.get(d, "path").(string),
RawQuery: bcr.get(d, "query").(string),
Opaque: bcr.get(d, "opaque").(string),
Fragment: bcr.get(d, "fragment").(string),
Scheme: bcr.get(dataMap, "scheme").(string),
Host: bcr.get(dataMap, "host").(string),
Path: bcr.get(dataMap, "path").(string),
RawQuery: bcr.get(dataMap, "query").(string),
Opaque: bcr.get(dataMap, "opaque").(string),
Fragment: bcr.get(dataMap, "fragment").(string),
}
userinfo := bcr.get(d, "userinfo").(string)
userinfo := bcr.get(dataMap, "userinfo").(string)
var user *url.Userinfo
if userinfo != "" {
tempURL, err := url.Parse(fmt.Sprintf("proto://%s@host", userinfo))
Expand Down Expand Up @@ -121,9 +121,9 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, err
//
// Note: This function currently lacks error handling
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: getHostByName].
//
// {{ getHostByName "example.com" }} // Output: "237.84.2.178"
// [Sprout Documentation: getHostByName]: https://docs.atom.codes/sprout/registries/checksum#gethostbyname
func (bcr *BackwardCompatibilityRegistry) GetHostByName(name string) (string, error) {
addrs, err := net.LookupHost(name)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions registry/backward/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package backward

// get retrieves the value associated with the specified key from the given dictionary.
// If the key exists, it returns the corresponding value; otherwise, it returns an empty string.
//
// Parameters:
//
// dict map[string]any - the dictionary to search for the key.
// key string - the key whose associated value is to be returned.
//
// Returns:
//
// any - the value associated with the specified key, or an empty string if the key does not exist.
func (bcr *BackwardCompatibilityRegistry) get(dict map[string]any, key string) any {
if value, ok := dict[key]; ok {
return value
Expand Down
70 changes: 35 additions & 35 deletions registry/checksum/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,87 +10,87 @@ import (
"hash/adler32"
)

// SHA1Sum calculates the SHA-1 hash of the input string and returns it as a
// SHA1Sum calculates the SHA-1 hash of the value string and returns it as a
// hexadecimal encoded string.
//
// Parameters:
// - input: the string to be hashed.
// - value: the string to be hashed.
//
// Returns:
// - the SHA-1 hash of the input string as a hexadecimal encoded string.
// - the SHA-1 hash of the value string as a hexadecimal encoded string.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: sha1Sum].
//
// {{ sha1Sum "Hello, World!" }} // Output: 0a0a9f2a6772942557ab5355d76af442f8f65e01
func (cr *ChecksumRegistry) SHA1Sum(input string) string {
hash := sha1.Sum([]byte(input))
// [Sprout Documentation: sha1Sum]: https://docs.atom.codes/sprout/registries/checksum#sha1sum
func (cr *ChecksumRegistry) SHA1Sum(value string) string {
hash := sha1.Sum([]byte(value))
return hex.EncodeToString(hash[:])
}

// SHA256Sum calculates the SHA-256 hash of the input string and returns it as a
// SHA256Sum calculates the SHA-256 hash of the value string and returns it as a
// hexadecimal encoded string.
//
// Parameters:
// - input: the string to be hashed.
// - value: the string to be hashed.
//
// Returns:
// - the SHA-256 hash of the input string as a hexadecimal encoded string.
// - the SHA-256 hash of the value string as a hexadecimal encoded string.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: sha256Sum].
//
// {{ sha256Sum "Hello, World!" }} // Output: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
func (cr *ChecksumRegistry) SHA256Sum(input string) string {
hash := sha256.Sum256([]byte(input))
// [Sprout Documentation: sha256Sum]: https://docs.atom.codes/sprout/registries/checksum#sha256sum
func (cr *ChecksumRegistry) SHA256Sum(value string) string {
hash := sha256.Sum256([]byte(value))
return hex.EncodeToString(hash[:])
}

// SHA512Sum calculates the SHA-512 hash of the input string and returns it as a
// SHA512Sum calculates the SHA-512 hash of the value string and returns it as a
// hexadecimal encoded string.
//
// Parameters:
// - input: the string to be hashed.
// - value: the string to be hashed.
//
// Returns:
// - the SHA-512 hash of the input string as a hexadecimal encoded string.
// - the SHA-512 hash of the value string as a hexadecimal encoded string.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: sha512Sum].
//
// {{ sha512Sum "Hello, World!" }} // Output: 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
func (cr *ChecksumRegistry) SHA512Sum(input string) string {
hash := sha512.Sum512([]byte(input))
// [Sprout Documentation: sha512Sum]: https://docs.atom.codes/sprout/registries/checksum#sha512sum
func (cr *ChecksumRegistry) SHA512Sum(value string) string {
hash := sha512.Sum512([]byte(value))
return hex.EncodeToString(hash[:])
}

// Adler32Sum calculates the Adler-32 checksum of the input string and returns
// Adler32Sum calculates the Adler-32 checksum of the value string and returns
// it as a hexadecimal encoded string.
//
// Parameters:
// - input: the string to be hashed.
// - value: the string to be hashed.
//
// Returns:
// - the Adler-32 checksum of the input string as a hexadecimal encoded string.
// - the Adler-32 checksum of the value string as a hexadecimal encoded string.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: adler32Sum].
//
// {{ adler32Sum "Hello, World!" }} // Output: 1f9e046a
func (cr *ChecksumRegistry) Adler32Sum(input string) string {
hash := adler32.Checksum([]byte(input))
// [Sprout Documentation: adler32Sum]: https://docs.atom.codes/sprout/registries/checksum#adler32sum
func (cr *ChecksumRegistry) Adler32Sum(value string) string {
hash := adler32.Checksum([]byte(value))
return fmt.Sprint(hash)
}

// MD5Sum calculates the MD5 hash of the input string and returns it as a
// MD5Sum calculates the MD5 hash of the value string and returns it as a
// hexadecimal encoded string.
//
// Parameters:
// - input: the string to be hashed.
// - value: the string to be hashed.
//
// Returns:
// - the MD5 hash of the input string as a hexadecimal encoded string.
// - the MD5 hash of the value string as a hexadecimal encoded string.
//
// Example:
// For an example of this function in a Go template, refer to [Sprout Documentation: md5Sum].
//
// {{ md5Sum "Hello, World!" }} // Output: 65a8e27d8879283831b664bd8b7f0ad4
func (cr *ChecksumRegistry) MD5Sum(input string) string {
hash := md5.Sum([]byte(input))
// [Sprout Documentation: md5Sum]: https://docs.atom.codes/sprout/registries/checksum#md5sum
func (cr *ChecksumRegistry) MD5Sum(value string) string {
hash := md5.Sum([]byte(value))
return hex.EncodeToString(hash[:])
}
1 change: 0 additions & 1 deletion registry/checksum/helpers.go

This file was deleted.

Loading

0 comments on commit a3026c2

Please sign in to comment.