gqldecode

package
v0.0.0-...-3804d40 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package gqldecode provides a decoder that unmarshals GraphQL input arguments into a struct. The struct for the input arguments is annotated with tags similar to the stdlib json parser, but instead of "json" the key "gql" is used. The options "nonzero" and "plane0" can be used to signify that the field should not be the zero value and that a string field must be plan0 utf8 (i.e. no emoji).

Example
type Artist struct {
	Name   string   `gql:"name,nonzero,plane0"`
	Age    int      `gql:"age"`
	Albums []string `gql:"albums"`
}
args := map[string]any{
	"name": "Jimi Hendrix",
	"age":  75,
	"albums": []any{
		"Are You Experienced",
		"Axis: Bold as Love",
		"Electric Ladyland",
	},
}
var artist Artist
err := Decode(args, &artist)
if err != nil {
	fmt.Println("error:", err)
}
fmt.Printf("%+v", artist)
Output:

{Name:Jimi Hendrix Age:75 Albums:[Are You Experienced Axis: Bold as Love Electric Ladyland]}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(in map[string]any, out any) (err error)

Decode parses a map of strings to interfaces, as provided by the graphql library, into the provided out interface.

func IsValidPlane0Unicode

func IsValidPlane0Unicode(s string) bool

IsValidPlane0Unicode returns true iff the provided string only has valid plane 0 unicode (no emoji)

Types

type Decoder

type Decoder interface {
	DecodeGQL(any) error
}

Decoder is the interface implemented by types that know how to decode themselves.

type ValidationFailedError

type ValidationFailedError struct {
	Field  string
	Reason string
}

ValidationFailedError is returned if the input doesn't match the expected output format.

func (*ValidationFailedError) Error

func (e *ValidationFailedError) Error() string

Jump to

Keyboard shortcuts

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