🌱
Sprout
Github
  • About
  • Roadmap to Sprout v1.0
  • Migration from Sprig
  • Introduction
    • Getting Started
    • Templating Conventions
  • Features
    • Loader System (Registry)
    • Loader System (Registry Group)
    • Function Aliases
    • Function Notices
    • Safe Functions
  • Registries
    • List of all registries
    • Backward
    • Checksum
    • Conversion
    • Crypto
    • Encoding
    • Env
    • Filesystem
    • Maps
    • Numeric
    • Network
    • Random
    • Reflect
    • Regexp
    • SemVer
    • Slices
    • Std
    • Strings
    • Time
    • Uniqueid
  • Groups
    • List of all registry groups
    • All
    • Hermetic
  • Advanced
    • How to create a handler
    • How to create a registry
    • How to create a registry group
  • Links
    • GitHub repository
Powered by GitBook
On this page
  • typeIs
  • typeIsLike
  • typeOf
  • kindIs
  • kindOf
  • hasField
  • deepEqual
  • deepCopy
Edit on GitHub
  1. Registries

Reflect

The Reflect registry offers tools for inspecting and manipulating data types using reflection, enabling advanced dynamic type handling within your projects.

You can easily import all the functions from the reflect registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/reflect"

typeIs

The function compares the type of the provided value (src) to a specified target type string (target). It returns true if the type of src matches the target type.

Signature

{{ 42 | typeIs "int" }} // Output: true

typeIsLike

The function compares the type of the provided value (src) to a target type string (target), with an option for a wildcard * prefix (pointer). It returns true if src matches target or *target, which is useful for checking if a variable is of a specific type or a pointer to that type.

Signature

{{ 42 | typeIsLike "*int" }} // Output: false

typeOf

The function returns the type of the provided value (src) as a string, giving you a textual representation of its data type.

Signature

{{ 42 | typeOf }} // Output: "int"

kindIs

The function compares the kind (category) of a given value (src) to a target kind string (target). It returns true if the kind of src matches the specified target kind.

Signature

{{ 42 | kindIs "int" }} // Output: true
{{ nil | kindIs "int" }} // Error

kindOf

The function returns the kind (category) of the provided value (src) as a string, giving a general classification like "int," "struct," or "slice."

Signature

{{ 42 | kindOf }} // Output: "int"
{{ nil | kindOf }} // Error

hasField

The function checks the presence of a field with the specified name (name) in the provided struct (src). It returns true if the field exists.

Signature

Assuming Struct has defined as : type Struct struct { V string }

{{ hasField "V" .Struct }} // Output: true
{{ hasField "someNonExistingField" .Struct }} // Output: false
{{ .Struct | hasField "V" }} // Output: true
{{ .Struct | hasField "someNonExistingField" }} // Output: false

deepEqual

The function checks if two variables, x and y, are deeply equal by comparing their values and structures using reflect.DeepEqual.

Signature

{{ .Struct | deepEqual .Struct }} // Output: true
{{ .Struct | deepEqual .SecondStruct }} // Output: false

deepCopy

Signature

{{ dict "name" "John" | deepCopy }} // Output: map[name:John]
{{ nil | deepCopy }} // Error
PreviousRandomNextRegexp

Last updated 5 months ago

The function performs a deep copy of the provided element, creating an exact duplicate of its structure and data. It uses MustDeepCopy internally to manage the copy process and handle any potential errors. This use the internally.

TypeIs(target string, src any) bool
TypeIsLike(target string, src any) bool
TypeOf(src any) string
KindIs(target string, src any) (bool, error)
KindOf(src any) (string, error)
HasField(name string, src any) (bool, error)
DeepEqual(x, y any) bool
DeepCopy(element any) (any, error)
copystructure package