textx

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 9 Imported by: 3 modules, 10 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.

Documentation

Overview

Code generated by genx:code DO NOT EDIT.

Index

Examples

Constants

View Source
const URLTag = "url"

URLTag describes url parameter in struct tag

Variables

View Source
var (
	TextMarshaler   = reflect.TypeFor[encoding.TextMarshaler]()
	TextUnmarshaler = reflect.TypeFor[encoding.TextUnmarshaler]()
)

Functions

func Marshal added in v0.1.0

func Marshal(v any) ([]byte, error)

func MarshalURL added in v0.1.0

func MarshalURL(v any) (url.Values, error)

MarshalURL encodes a struct value into url.Values. It supports struct tags `name:"customName" as the URL parameter name; It will skip unexported fields and fields with name tag "-". By default, the URL parameter names are converted to lowerCamelCase.

Example
u, err := MarshalURL(struct {
	Name    string `url:"id"`
	Age     int
	Gender  int8
	Country string `url:"country,default='cn'"`
	Codes   []string
}{
	Name:  "Alex",
	Age:   30,
	Codes: []string{"a", "b", "c"},
})

if err != nil {
	panic(err)
}
fmt.Println(u.Encode())
Output:
age=30&codes=a&codes=b&codes=c&country=cn&id=Alex

func SetDefault added in v0.3.4

func SetDefault(v any) (url.Values, error)
Example
fmt.Println("=> need struct value for marshaling")
_, err := SetDefault(1)
fmt.Println(err.Error())

var config = struct {
	K1 int    `url:",default=100"`
	K2 string `url:",default=v2"`
}{K1: 101}

fmt.Println("=> initialize a mutable(pointer) struct")
u, _ := SetDefault(&config)
fmt.Printf("url parameter values: %s", u.Encode())
Output:
=> need struct value for marshaling
[textx:3] marshal url got invalid input. expect struct type
=> initialize a mutable(pointer) struct
url parameter values: k1=101&k2=v2

func Unmarshal added in v0.1.0

func Unmarshal(data []byte, v any) error

func UnmarshalURL added in v0.1.0

func UnmarshalURL(u url.Values, v any) error

UnmarshalURL decodes values from url.Values into a struct. The input MUST be a pointer to a struct or reflect.Value of a struct.

Types

type Ecode added in v0.2.0

type Ecode int8

Ecode defines error code when Marshal/Unmarshal +genx:code @def textx

const (
	ECODE_UNDEFINED                     Ecode = iota
	ECODE__MARSHAL_TEXT_INVALID_INPUT         // marshal text got invalid input
	ECODE__MARSHAL_TEXT_FAILED                // failed to marshal text
	ECODE__MARSHAL_URL_INVALID_INPUT          // marshal url got invalid input
	ECODE__MARSHAL_URL_FAILED                 // failed to marshal url
	ECODE__UNMARSHAL_TEXT_INVALID_INPUT       // unmarshal text got invalid input
	ECODE__UNMARSHAL_TEXT_FAILED              // failed to unmarshal text
	ECODE__UNMARSHAL_URL_INVALID_INPUT        // unmarshal url got invalid input
	ECODE__UNMARSHAL_URL_FAILED               // failed to unmarshal url
)

func (Ecode) Message added in v0.2.0

func (e Ecode) Message() string

Jump to

Keyboard shortcuts

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