Documentation
¶
Overview ¶
Package apikit holds what every package of api-kit shares: the kinds of errors the engine reports and the exit codes a command-line tool maps them to.
api-kit turns a declarative description of a REST API into a command-line tool. The packages below this one never print or exit; they return errors that carry a Kind, and package cli decides what to print and which exit code to use.
Index ¶
- Constants
- func Clone(v any) any
- func Decode(r io.Reader) (any, error)
- func Equal(a, b any) bool
- func ExitCode(err error) int
- func Falsy(v any) bool
- func Marshal(v any) ([]byte, error)
- func Normalize(v any) (any, error)
- func Text(v any) string
- func Unmarshal(data []byte) (any, error)
- type Error
- type Kind
- type Object
- func (o *Object) All() iter.Seq2[string, any]
- func (o *Object) Delete(key string)
- func (o *Object) Get(key string) (any, bool)
- func (o *Object) Has(key string) bool
- func (o *Object) Keys() []string
- func (o *Object) Len() int
- func (o *Object) MarshalJSON() ([]byte, error)
- func (o *Object) Set(key string, value any)
- func (o *Object) UnmarshalJSON(data []byte) error
Constants ¶
const ( // ExitOK is the exit code of a successful run. ExitOK = 0 // ExitFailed is the exit code of a failed run. ExitFailed = 1 // ExitUsage is the exit code for a mistake on the command line. ExitUsage = 2 // ExitSkipped is the exit code when nothing was done on purpose. ExitSkipped = 3 // ExitNotFound is the exit code when the resource does not exist. ExitNotFound = 4 // ExitCanceled is the exit code of an interrupted run, 128 plus the // number of the interrupt signal. ExitCanceled = 130 )
Exit codes a command-line tool ends with.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
Clone returns a deep copy of a value of the types Decode produces. Scalars are returned as they are.
func Decode ¶
Decode reads one JSON value from r. Objects become *Object and keep the order of their keys, numbers become json.Number and keep their exact spelling, arrays become []any (never nil). Anything but white space after the value is an error.
func Equal ¶
Equal reports whether two values of the types Decode produces are the same data: objects have the same keys with equal values, whatever their order; lists have equal elements in the same order; numbers are compared by value, so 1, 1.0 and 1e0 are equal.
func Falsy ¶
Falsy reports whether a value of the types Decode produces counts as "nothing there": null, false, the empty string, a number equal to zero, an empty list or an empty object.
func Marshal ¶
Marshal returns the compact JSON encoding of v. Unlike json.Marshal it leaves <, > and & as they are: the result is sent to an API or shown in a terminal, not embedded in HTML.
func Normalize ¶
Normalize converts a value built from ordinary Go types into the types Decode produces: every integer and floating-point type becomes a json.Number, map[string]any becomes an *Object with its keys sorted, and slices become []any. Values that already have those types are returned as they are, lists and objects are converted in depth. It returns an error for a value JSON cannot hold, such as a channel or a NaN.
Types ¶
type Error ¶
type Error struct {
// Kind classifies the error.
Kind Kind
// Msg is the message shown to the user.
Msg string
// Err is the underlying error, or nil.
Err error
}
Error is an error with a Kind. Its message is complete as it stands: it carries no "error:" prefix and does not name the program.
type Kind ¶
type Kind uint8
Kind classifies an error by what a command-line tool does about it.
const ( // KindFailed is any failure that has no more specific kind: a request // the API refused, an unreadable file, an unexpected response. KindFailed Kind = iota + 1 // KindUsage is a mistake on the command line: an unknown option, a // missing argument, values that may not be combined. KindUsage // KindSkipped means nothing was done on purpose: a condition the caller // set does not hold, or a result that had to be non-empty is empty. KindSkipped // KindNotFound means the resource asked for does not exist. KindNotFound // KindUnauthenticated means no usable credential is available. KindUnauthenticated // KindDefinition is a mistake in a definition document. KindDefinition // KindCanceled means the run was interrupted. KindCanceled )
The kinds of errors. The zero Kind means "no error".
func KindOf ¶
KindOf returns the kind of err: 0 for nil, the kind of the first *Error in its chain, KindCanceled for a canceled context, and KindFailed for anything else.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is a JSON object that keeps its keys in insertion order. API responses, payloads and definition documents are held as Objects, because the order of keys shows up in output and decides which of several rules applies first.
The values of an Object, and of the lists inside it, are of the types Decode produces: nil, bool, string, json.Number, []any and *Object.
An Object is not safe for concurrent use while it is modified.
func (*Object) All ¶
All iterates over the keys and values in insertion order. The object must not be modified during the iteration.
func (*Object) Get ¶
Get returns the value of key and whether the key is present. It is safe to call on a nil Object, which has no keys.
func (*Object) MarshalJSON ¶
MarshalJSON encodes the object with its keys in insertion order.
func (*Object) Set ¶
Set stores value under key. A new key is appended; an existing key keeps its position.
func (*Object) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON object, keeping the order of its keys. A key that occurs twice keeps its first position and its last value.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apitest provides a fake API for tests of code that talks to a REST API through api-kit: of the engine itself, and of the definitions other tools embed.
|
Package apitest provides a fake API for tests of code that talks to a REST API through api-kit: of the engine itself, and of the definitions other tools embed. |
|
Package auth supplies the token that the requests of a tool carry.
|
Package auth supplies the token that the requests of a tool carry. |
|
Package cli builds the command tree of a command-line tool and runs it.
|
Package cli builds the command tree of a command-line tool and runs it. |
|
argv
Package argv rewrites a command line into the form cobra parses, so that spellings cobra does not know keep working:
|
Package argv rewrites a command line into the form cobra parses, so that spellings cobra does not know keep working: |
|
Package definition reads the declarative description of an API that the rest of api-kit works from.
|
Package definition reads the declarative description of an API that the rest of api-kit works from. |
|
Package engine talks to the API a definition describes.
|
Package engine talks to the API a definition describes. |
|
examples
|
|
|
petstore
command
Command petstore is a complete command-line tool for an invented API.
|
Command petstore is a complete command-line tool for an invented API. |
|
Package expr holds the three small languages that API definitions and command lines are written in: templates, paths and conditions.
|
Package expr holds the three small languages that API definitions and command lines are written in: templates, paths and conditions. |
|
Package output writes the results of a command in the format the user asked for with --output.
|
Package output writes the results of a command in the format the user asked for with --output. |