marshal

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package marshal provides a representation of the command line model in a serializable data model.

Index

Constants

This section is empty.

Variables

View Source
var CodecRegistry = &provider.Registry{
	Name:      "codec",
	Providers: codecLookup{},
}

CodecRegistry provides a provider.Registry that enumerates the supported codecs. Each codec is instanced from a factory that takes Options as its argument, and only codecs which have been registered are listed. Use ListCodecs to expose the registry via a flag.

Functions

func ContextValue added in v0.18.0

func ContextValue(v *CodecProvider) cli.Action

ContextValue provides an action that sets the given value into the context. The only supported type is *CodecProvider.

func DisallowUnknownFields added in v0.14.1

func DisallowUnknownFields() codec.Option

DisallowUnknownFields affects unmarshaling and prevents unknown fields from being specified.

func Dump added in v0.18.0

func Dump(v ...any) cli.Action

Dump provides an action which dumps out the specified values to stdout using the codec in the context. See DumpContext.

func DumpContext added in v0.18.0

func DumpContext(ctx context.Context, v ...any) error

DumpContext prints the specified values to stdout. If the context provides the CLI context, then the stdout writer specified by it will be used; otherwise, os.Stdout will be used. The codec to use will be retrieved from the context; however, as a special case, if the first value is itself is a codec.Interface (or merely implements its MarshalWrite method), this specifies the codec to use rather than the one in the context. New lines separate each item.

func EscapeHTML added in v0.18.0

func EscapeHTML() codec.Option

EscapeHTML affects marshaling and generates escaped HTML within JSON. For other codecs, this option generates an error.

func From

func From(v any, opts ...Option) any

From creates the schema serialization value for the given Joe value. The following types are supported:

  • *cli.App
  • *cli.Arg
  • *cli.Command
  • *cli.Flag

Any other type of value specified will panic

func ListCodecs added in v0.18.0

func ListCodecs() cli.Action

ListCodecs provides an action that lists the supported codecs then exits. This action only works if the Registry has been installed into the context; otherwise, it produces an error

func RegisterCodec added in v0.14.1

func RegisterCodec(c Codec, f func() codec.Interface)

RegisterCodec provides the behavior of registering a codec. This is expected to be called by implementations in their package initializer

func SetOutput added in v0.18.0

func SetOutput(v ...Codec) cli.Action

SetOutput provides a flag which sets the codec to use for dumping

func SetOutputArgument added in v0.18.0

func SetOutputArgument(v ...Codec) cli.Action

SetOutputArgument provides a flag which sets an argument on the codec uses for dumping

func WithIndent added in v0.18.0

func WithIndent(indent string) codec.Option

WithIndent affects marshaling and sets the string used for each level of indentation in the encoded output.

Types

type App

type App struct {
	Name       string         `json:"name"`
	Commands   []Command      `json:"commands,omitempty"`
	Flags      []Flag         `json:"flags,omitempty"`
	Args       []Arg          `json:"args,omitempty"`
	HelpText   string         `json:"helpText,omitempty"`
	ManualText string         `json:"manualText,omitempty"`
	UsageText  string         `json:"usageText,omitempty"`
	Version    string         `json:"version,omitempty"`
	BuildDate  time.Time      `json:"buildDate"`
	Author     string         `json:"author,omitempty"`
	Copyright  string         `json:"copyright,omitempty"`
	License    string         `json:"license,omitempty"`
	Comment    string         `json:"comment,omitempty"`
	Options    Options        `json:"options,omitempty"`
	Data       map[string]any `json:"data,omitempty"`
}

App provides a representation of cli.App for use as data

type Arg

type Arg struct {
	Name        string         `json:"name"`
	EnvVars     []string       `json:"envVars,omitempty"`
	FilePath    string         `json:"filePath,omitempty"`
	HelpText    string         `json:"helpText,omitempty"`
	ManualText  string         `json:"manualText,omitempty"`
	Category    string         `json:"category,omitempty"`
	UsageText   string         `json:"usageText,omitempty"`
	DefaultText string         `json:"defaultText,omitempty"`
	Options     Options        `json:"options,omitempty"`
	Data        map[string]any `json:"data,omitempty"`
}

Arg provides a representation of cli.Arg for use as data

type BuiltinType added in v0.17.0

type BuiltinType int

BuiltinType identifies the built-in supported types

const (
	UnknownType BuiltinType = iota

	BigFloat
	BigInt
	Bool
	Bytes
	Duration
	File
	FileSet
	Float32
	Float64
	Int
	Int16
	Int32
	Int64
	Int8
	IP
	List
	Map
	NameValue
	NameValues
	Regexp
	String
	Uint
	Uint16
	Uint32
	Uint64
	Uint8
	URL
)

The various types that the CLI supports

func (BuiltinType) MarshalText added in v0.17.0

func (t BuiltinType) MarshalText() ([]byte, error)

MarshalText provides the textual representation

func (BuiltinType) New added in v0.17.0

func (t BuiltinType) New() any

func (*BuiltinType) Set added in v0.17.0

func (t *BuiltinType) Set(arg string) error

func (BuiltinType) String added in v0.17.0

func (t BuiltinType) String() string

func (*BuiltinType) UnmarshalText added in v0.17.0

func (t *BuiltinType) UnmarshalText(b []byte) error

UnmarshalText converts the textual representation

type Codec added in v0.14.1

type Codec int

Codec identifies the support codecs. The JSON codec is supported by default. To add support for additional codecs, you must import them or register them. For example,

import _ "github.com/Carbonfrost/joe-cli/extensions/marshal/codec/toml"
const (
	JSON Codec = iota
	YAML
	TOML
)

The available formats for marshaling and unmarshaling data

func (Codec) Available added in v0.14.1

func (c Codec) Available() bool

Available indicates whether the codec type is registered

func (Codec) New added in v0.14.1

func (c Codec) New(opts ...codec.Option) (codec.Interface, error)

New creates an instance of the given codec

func (Codec) String added in v0.14.1

func (c Codec) String() string

String provides the name of the codec

type CodecProvider added in v0.18.0

type CodecProvider struct {
	cli.Action
	// contains filtered or unexported fields
}

CodecProvider provides the context-bound provider that can be used as a codec.

func CodecProviderFromContext added in v0.18.0

func CodecProviderFromContext(ctx context.Context) *CodecProvider

CodecProviderFromContext retrieves the codec provider from the context

func NewCodecProvider added in v0.18.0

func NewCodecProvider(opts ...CodecProviderOption) *CodecProvider

NewCodecProvider provides a value that provides the codec to use when dumping. By default, adding the provider to the pipeline adds it as a context service which facilitates configuring the codec used by Dump

func (*CodecProvider) Apply added in v0.18.0

func (c *CodecProvider) Apply(opts ...CodecProviderOption)

Apply will apply the given options to the provider

func (*CodecProvider) Codec added in v0.18.0

func (c *CodecProvider) Codec() codec.Interface

Codec gets the codec used internally by the provider

func (*CodecProvider) MarshalWrite added in v0.18.0

func (c *CodecProvider) MarshalWrite(w io.Writer, in any) error

MarshalWrite provides marshaling by delegating to the internal codec if it exists. JSON is used by default

func (*CodecProvider) Pipeline added in v0.18.0

func (c *CodecProvider) Pipeline() cli.Action

func (*CodecProvider) SetCodec added in v0.18.0

func (c *CodecProvider) SetCodec(in codec.Interface)

SetCodec sets the codec used internally by the provider

func (*CodecProvider) UnmarshalRead added in v0.18.0

func (c *CodecProvider) UnmarshalRead(r io.Reader, out any) error

UnmarshalRead provides unmarshaling by delegating to the internal codec if it exists. JSON is used by default

type CodecProviderOption added in v0.18.0

type CodecProviderOption func(*CodecProvider)

CodecProviderOption provides options for the provider

func WithAction added in v0.18.0

func WithAction(a cli.Action) CodecProviderOption

WithAction sets the action to use with the codec

func WithDefaultAction added in v0.18.0

func WithDefaultAction() CodecProviderOption

WithDefaultAction sets the action to the default, which sets the CodecProvider into the context and sets up the flags: SetOutput, SetOutputArgument, and ListCodecs

type Command

type Command struct {
	Name        string         `json:"name"`
	Aliases     []string       `json:"aliases,omitempty"`
	Subcommands []Command      `json:"subcommands,omitempty"`
	Flags       []Flag         `json:"flags,omitempty"`
	Args        []Arg          `json:"args,omitempty"`
	HelpText    string         `json:"helpText,omitempty"`
	ManualText  string         `json:"manualText,omitempty"`
	UsageText   string         `json:"usageText,omitempty"`
	Comment     string         `json:"comment,omitempty"`
	Category    string         `json:"category,omitempty"`
	Options     Options        `json:"options,omitempty"`
	Data        map[string]any `json:"data,omitempty"`
}

Command provides a representation of cli.Command for use as data

type Flag

type Flag struct {
	Name        string         `json:"name"`
	Aliases     []string       `json:"aliases,omitempty"`
	EnvVars     []string       `json:"envVars,omitempty"`
	FilePath    string         `json:"filePath,omitempty"`
	HelpText    string         `json:"helpText,omitempty"`
	ManualText  string         `json:"manualText,omitempty"`
	Category    string         `json:"category,omitempty"`
	UsageText   string         `json:"usageText,omitempty"`
	DefaultText string         `json:"defaultText,omitempty"`
	Options     Options        `json:"options,omitempty"`
	Data        map[string]any `json:"data,omitempty"`
}

Flag provides a representation of cli.Flag for use as data

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specifies an option for creating marshal values

func WithPrivateData added in v0.10.0

func WithPrivateData() Option

WithPrivateData provides an option that causes private data, which is any data added to a Data map whose key starts with understcore, is included in the marshal representation of a target. By default, private data is excluded

type Options added in v0.10.0

type Options = cli.Option

Options provides a representation of cli.Option for use as data

type Schema added in v0.17.0

type Schema map[string]Type

Schema represents a structural type with named fields

func (Schema) MarshalJSON added in v0.17.0

func (s Schema) MarshalJSON() ([]byte, error)

MarshalJSON provides JSON representation of the schema

func (Schema) MarshalText added in v0.17.0

func (s Schema) MarshalText() ([]byte, error)

MarshalText provides the textual representation

func (Schema) New added in v0.17.0

func (s Schema) New() any

New creates a new instance based on the schema

func (Schema) String added in v0.17.0

func (s Schema) String() string

String returns a string representation of the schema

func (*Schema) UnmarshalJSON added in v0.17.0

func (s *Schema) UnmarshalJSON(b []byte) error

UnmarshalJSON parses JSON representation into a schema

type Type

type Type interface {
	New() any
	String() string
	MarshalText() ([]byte, error)
}

Type represents a type that can be used in the CLI

Directories

Path Synopsis
Package codec provides a model and conventions for marshaling and unmarshaling values to and from their encodings.
Package codec provides a model and conventions for marshaling and unmarshaling values to and from their encodings.
toml
Package toml provides the TOML codec
Package toml provides the TOML codec

Jump to

Keyboard shortcuts

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