mustbe

package
v5.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

MustBe

mustbe.* functions process an argument of any type and convert it to the required type.

  • Any validation steps must be done here. Any range checking, keyword restrictions, etc. should be done here.
  • Any IDNA (PunyCode) conversions must be done here. Only ASCII is stored in fields that hold hostnames.
  • Any hostnames must be extended to their FQDN, including a trailing dot (if required).
  • If a field is case insensitive, we convert it to lowercase (hostnames) or uppercase (base64 or similar) so that future comparisons can simply use ==.

In short, by the time any data makes it to into the struct, it must be valid. No later validation is done. (That's not entirely true. pkg/normalize/ still does some validation/normalization but once all DNS record types have a Make*() function, we'll be removing validation steps from pkg/normalize/validate. That is, in the future, pkg/normalize/ will do per-zone validation only, all per-record validations will be done in the Make*() functions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(a any) bool

Bool converts to a bool.

func BoolString

func BoolString(a any) string

BoolString converts to a string that is either `true` or `false`.

func CheckTargetChars added in v5.10.0

func CheckTargetChars(target string) error

CheckTargetChars returns an error if target contains characters that are not valid in a hostname. "/" is permitted in reverse (in-addr.arpa) names for RFC 2317 classless delegation.

func Float32

func Float32(arg any) float32

Float32 converts arg to a float32.

func Float64

func Float64(arg any) float64

Float64 converts arg to a float64.

func IPv4

func IPv4(a any) (netip.Addr, error)

IPv4 converts a to an IPv4 address.

func IPv6

func IPv6(a any) (netip.Addr, error)

IPv6 converts a to an IPv6 address.

func Int64

func Int64(arg any) int64

Int64 converts arg to an int64.

func MakeErrorSingleDotViolation

func MakeErrorSingleDotViolation(s string) error

MakeErrorSingleDotViolation returns an error for a target missing its trailing dot.

func OpenPGPKey

func OpenPGPKey(a any) (string, error)

OpenPGPKey returns the key re-encoded as base64. The input may be hex or base64.

func RRType added in v5.10.0

func RRType(_ string, isEnabled nrc.Flags, arg any) (string, error)

RRType upcases a field that names a DNS record type (such as the type a LUA record emits). origin is unused; it is accepted so that the generated code can call this like TargetHost. With isEnabled.StrictParamChecking, the result must be a known DNS type.

func RawString

func RawString(a any) string

RawString converts a to a string as-is.

func SoaMailbox

func SoaMailbox(arg any) string

SoaMailbox format a SOA Mailbox string, such as tal.example.com (The "@" in the email address is written as an "." because nothing in DNS is simple). This function also... * Turns normal email addresses (tal@example.com) into SOA Mailbox strings (tal.example.com). * Turns Unicode into PunyCode (just in the hostname part) * Runs ToLower. * Adds the trailing dot if it is missing (tal.example.com -> tal.example.com.). * Does NOT add an origin if this is a shortname. We can't know if it is a shortname because it is dotless.)

func TargetHost

func TargetHost(origin string, isEnabled nrc.Flags, arg any) (string, error)

TargetHost returns a FQDN (or @) suitable as a target for CNAME and other records. What is a target? * A "target" is a hostname used in fields of a DNS record. For example, in "foo IN MX 10 bar.example.com.", "bar" is the target. * A target is stored as a FQDN+"." with all Unicode converted to ASCII (PunyCode). * It is never stored as "@" or a shortname. * If you require a shortname, use dc.TargetAsShort(target) or dc.TargetAsShortOrAt(target).

* origin must be a FQDN without a trailing dot. Violations cause a panic so that this is caught before code is shipped. * if origin is "", a special mode is activated that disables most action.

  • This is for legacy situations where the origin is unknown.
  • This will eventually be an error.
  • In this mode, Either the original string is returned, or if that string is "", "UNKNOWNORIGIN." is returned. This is the only situation where this function might return "@".

* Normal operation:

  • arg may be a string or it will be converted to a string using fmt.Printf("%v").
  • Unicode is converted to PunyCode.
  • The result always ends with a "."
  • The reason for not storing the short or "@" version is that "preview" output becomes ambiguous. Explicit is better than implicit.
  • Wildcards ("@") are rejected since they are not valid in targets. That's why this is called TargetHost and not Host.

* origin == ".":

  • This is a special flag for TargetIsFqdnNoDot mode.

* Examples: (assume $origin = "domain.com")

  • `@` -> $origin
  • `foo.$origin.` -> `foo.$origin.`
  • `$origin.` -> `$origin.`
  • `other.com.` -> `other.com.`
  • `short` -> `short.$origin`

* isEnabled.StrictParamChecking:

  • The result is rejected if it contains characters that are not valid in a hostname.

func TargetHostSRV

func TargetHostSRV(origin string, isEnabled nrc.Flags, arg any) (string, error)

TargetHostSRV is like TargetHost with the exception that "." and "" have special meaning and are left alone.

func ToLowerRawString added in v5.3.0

func ToLowerRawString(a any) string

ToLowerRawString downcases a field so that comparisons do not need to be case-aware.

func ToUpperRawString

func ToUpperRawString(a any) string

ToUpperRawString upcases a field so that comparisons do not need to be case-aware.

func Txts

func Txts(args ...any) []string

Txts joins args into a TXT string slice.

func Uint8

func Uint8(arg any) uint8

Uint8 converts arg to a uint8.

func Uint16

func Uint16(arg any) uint16

Uint16 converts arg to a uint16.

func Uint32

func Uint32(arg any) uint32

Uint32 converts arg to a uint32.

func Uint64

func Uint64(arg any) uint64

Uint64 converts arg to a uint64.

func ValidArgs

func ValidArgs(args []any)

ValidArgs panics if args looks like a variadic call that lost its spread operator somewhere up the call chain.

Many Make*() functions are declared `func(..., args ...any)` and forward to another variadic function. The forwarding call must use `f(..., args...)`. If the `...` is omitted, the called function sees args == []any{ []any{...} } i.e. a single element whose value is itself the original []any. The individual values are then never validated as strings/ints/etc., and the bug surfaces far from its cause.

ValidArgs catches exactly that shape: len(args) == 1 AND args[0] is a []any. We panic rather than return an error because the only way to produce this shape is a programmer mistake at a call site; tests will surface it loudly.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL