format

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 14 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.

Documentation

Overview

Package format is the file format of the HTTP client, version 1 (SPEC §10.33 "The format"): the types of a collection, a folder, a request and an environment, reading them from text with comments, and writing them.

Reading accepts comments and trailing commas (extension/jsonc). Writing a file that exists goes through the edits of the comment-keeping JSON editor, so the comments, the layout and the keys a user added by hand stay; a new file is written with the keys in the fixed order of the SPEC, two spaces and a final line break. A file of a newer version is read, and never written. A secret variable never has its value in text (D45).

Nothing here knows the workspace except ResolvePath and the file helpers, which keep every path inside the workspace folder.

Index

Constants

View Source
const (
	// Dir is the folder of the HTTP client in the workspace folder.
	Dir = ".emetos/http"
	// CollectionsDir and EnvironmentsDir are the folders of Dir that hold the collections and the environments.
	CollectionsDir  = "collections"
	EnvironmentsDir = "environments"
	// CollectionFile and FolderFile are the files that describe a collection and a folder of one.
	CollectionFile = "collection.jsonc"
	FolderFile     = "folder.jsonc"
	// RequestSuffix and EnvironmentSuffix end the file name of a request and of an environment.
	RequestSuffix     = ".request.jsonc"
	EnvironmentSuffix = ".jsonc"
)

The folders and file names of a project (SPEC §10.33).

View Source
const (
	AuthInherit = "inherit"
	AuthNone    = "none"
	AuthBasic   = "basic"
	AuthBearer  = "bearer"
	AuthAPIKey  = "apikey"
	AuthDigest  = "digest"
	AuthOAuth2  = "oauth2"
)

The types of an Auth.

View Source
const (
	BodyNone       = "none"
	BodyJSON       = "json"
	BodyXML        = "xml"
	BodyText       = "text"
	BodyHTML       = "html"
	BodyJavaScript = "javascript"
	BodyGraphQL    = "graphql"
	BodyForm       = "form"
	BodyMultipart  = "multipart"
	BodyBinary     = "binary"
)

The types of a Body.

View Source
const MaxFileSize = 8 << 20

MaxFileSize is the largest file the client reads.

View Source
const MaxNameLength = 100

MaxNameLength is the most bytes of a safe name.

View Source
const Version = 1

Version is the version of the format this package reads and writes (the value of the key "emetosHttp").

Variables

This section is empty.

Functions

func BodyExtension

func BodyExtension(bodyType string) string

BodyExtension is the extension of the body file of a body type, with its dot; it is empty for a type that has no body file.

func BodyFileName

func BodyFileName(stem, bodyType string) string

BodyFileName is the name of the body file of the request with the stem, or "" for a type with no body file.

func CheckPaths

func CheckPaths(folder string, r Request) error

CheckPaths checks every file path a request names (the path of a binary body and the files of multipart fields) with ResolvePath, and returns the first error.

func EnvironmentFileName

func EnvironmentFileName(stem string) string

EnvironmentFileName is the file name of the environment whose stem is stem.

func IsNewer

func IsNewer(err error) bool

IsNewer reports whether err is the HttpFileNewerVersion of Parse or Update.

func Marshal

func Marshal[T Document](doc T) (string, error)

Marshal writes a new file for the document: the keys in the order of the SPEC, two spaces, a final line break. A secret variable has no value in it. It fails with HttpFileValueInvalid for a value the format does not allow (the key is in the metadata) and HttpFileEncodeFailed.

func Parse

func Parse[T Document](text string) (T, error)

Parse reads a file of the client from its text, with comments and trailing commas allowed. Keys it does not know are ignored.

A file of a newer version (emetosHttp above Version) is read as far as it can be and returned together with an error that has the tag HttpFileNewerVersion (IsNewer tells it): the caller shows the file read-only. The other errors, which come with the line and column in their metadata ("line", "column", "offset"), have the tags HttpFileSyntax (the text is not JSON with comments), HttpFileNotObject, HttpFileVersionMissing, HttpFileVersionInvalid and HttpFileValueInvalid (a key holds a value of the wrong shape, or one the format does not allow; "key" names it).

func ReadFile

func ReadFile(ctx context.Context, path string) (string, error)

ReadFile reads a file of the client as UTF-8 text. The errors have the tags HttpFileReadFailed (the cause is kept, so errors.Is(err, fs.ErrNotExist) tells a missing file), HttpFileTooLarge and HttpFileNotText.

func RequestFileName

func RequestFileName(stem string) string

RequestFileName is the file name of the request whose stem is stem.

func RequestStem

func RequestStem(fileName string) (string, bool)

RequestStem is the stem of a request file name, and whether the name is one.

func ResolvePath

func ResolvePath(folder, p string) (string, error)

ResolvePath returns the absolute path of p, a path relative to the workspace folder (an absolute one must lie inside it), and refuses a path that leaves the folder, by a ".." or by a link that points out of it. A path that does not exist yet is checked through its nearest folder that does. The errors have the tags HttpPathInvalid (empty, or with a NUL) and HttpPathOutsideWorkspace.

func SafeName

func SafeName(name string) string

SafeName makes a name fit for a file name (SPEC §10.33): lowercase, letters and digits and "_" kept, everything else (spaces, slashes, dots, punctuation) becomes "-", runs of "-" become one and none is left at the ends. A name with nothing left is "untitled". The result is never "." or "..", has no separator and is at most MaxNameLength bytes.

func UniqueName

func UniqueName(base string, taken func(name string) bool) string

UniqueName returns base, or base-2, base-3, ... for the first of them that taken does not report as taken.

func Update

func Update[T Document](oldText string, doc T) (string, error)

Update returns the text of a file that holds the document, made from the old text of the file by edits: a value that did not change is not touched, the comments and the layout stay, and so do the keys the format does not know and the unknown keys of the objects of the tables. A key the document lacks that the format knows is removed; a new key goes where the order of the SPEC puts it among the keys the file has. A secret variable's value is removed from the text.

It fails with HttpFileValueInvalid (a value the format does not allow), HttpFileSyntax, HttpFileNotObject (the old text is not a file of the client), HttpFileNewerVersion (the old file is of a newer version: it is never written), HttpFileEncodeFailed and the tags of the JSON editor.

func ValidSiblingName

func ValidSiblingName(name string) bool

ValidSiblingName reports whether name can be the name of a body file: a plain file name, no folders.

func VariablesFileName

func VariablesFileName(stem string) string

VariablesFileName is the name of the file of the GraphQL variables of the request with the stem.

func WriteFile

func WriteFile(ctx context.Context, path, text string) (err error)

WriteFile writes the text to the file at path atomically (a temporary file in the same folder, then a rename), making the folders it needs. The mode of a file that exists is kept; a new file is 0644. The errors have the tag HttpFileWriteFailed.

Types

type Auth

type Auth struct {
	Type         string `json:"type"`
	Username     string `json:"username,omitempty"`
	Password     string `json:"password,omitempty"`
	Token        string `json:"token,omitempty"`
	Key          string `json:"key,omitempty"`
	Value        string `json:"value,omitempty"`
	In           string `json:"in,omitempty"`
	Grant        string `json:"grant,omitempty"`
	TokenURL     string `json:"tokenUrl,omitempty"`
	AuthURL      string `json:"authUrl,omitempty"`
	ClientID     string `json:"clientId,omitempty"`
	ClientSecret string `json:"clientSecret,omitempty"`
	Scope        string `json:"scope,omitempty"`
	Audience     string `json:"audience,omitempty"`
	PKCE         bool   `json:"pkce,omitempty"`
	AddTo        string `json:"addTo,omitempty"`
}

Auth is the authorization of a collection, folder or request. Which fields count depends on Type.

type Body

type Body struct {
	Type      string `json:"type"`
	File      string `json:"file,omitempty"`
	Variables string `json:"variables,omitempty"`
	Fields    []Pair `json:"fields,omitempty"`
	Path      string `json:"path,omitempty"`
}

Body is the body of a request. The text of a raw body is in the file named File, next to the request file; the variables of a GraphQL body are in the file named Variables.

type Check

type Check struct {
	Subject string `json:"subject"`
	Path    string `json:"path,omitempty"`
	Header  string `json:"header,omitempty"`
	Op      string `json:"op"`
	Value   any    `json:"value,omitempty"`
}

Check is an assertion about a response.

type Collection

type Collection struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	// Order lists the entries (requests by file name, folders by folder name) in the order they are shown.
	Order     []string   `json:"order,omitempty"`
	Variables []Variable `json:"variables,omitempty"`
	Auth      *Auth      `json:"auth,omitempty"`
	Headers   []Pair     `json:"headers,omitempty"`
}

Collection is collection.jsonc, and (as Folder) folder.jsonc of a folder of a collection.

type Document

type Document interface {
	Collection | Request | Environment
}

Document is a type of file the client reads and writes: Collection (and Folder), Request and Environment.

type Environment

type Environment struct {
	Name      string     `json:"name"`
	Variables []Variable `json:"variables,omitempty"`
}

Environment is a file of environments/.

type ExtractRule

type ExtractRule struct {
	From   string `json:"from"`
	Path   string `json:"path,omitempty"`
	Header string `json:"header,omitempty"`
	Cookie string `json:"cookie,omitempty"`
	Regex  string `json:"regex,omitempty"`
	To     string `json:"to"`
	Scope  string `json:"scope,omitempty"`
}

ExtractRule takes a value out of a response into a variable.

type Folder

type Folder = Collection

Folder is folder.jsonc: it has the keys of a collection (a folder has variables too: SPEC §10.33 "As built (step 27.1)").

type Pair

type Pair struct {
	Name        string
	Value       string
	File        string
	Description string
	// Enabled is true unless the file says "enabled": false.
	Enabled bool
}

Pair is a name and a value of the tables of the request tab (a parameter, a header, a form field) and may have a description. A multipart field that sends a file has File and no Value.

func (Pair) MarshalJSON

func (p Pair) MarshalJSON() ([]byte, error)

MarshalJSON writes the pair with "enabled" only when it is false, and "value" unless the pair is a file.

func (*Pair) UnmarshalJSON

func (p *Pair) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a pair; a missing "enabled" is true.

type Position

type Position struct {
	Line, Column, Offset int
}

Position is a place in a text: the line and column (both from 1, the column counted in characters) and the byte offset.

func PositionOf

func PositionOf(text string, offset int) Position

PositionOf returns the place of the byte offset in the text.

type Request

type Request struct {
	Name        string        `json:"name"`
	Description string        `json:"description,omitempty"`
	Method      string        `json:"method"`
	URL         string        `json:"url"`
	Params      []Pair        `json:"params,omitempty"`
	Headers     []Pair        `json:"headers,omitempty"`
	Auth        *Auth         `json:"auth,omitempty"`
	Body        *Body         `json:"body,omitempty"`
	Settings    *Settings     `json:"settings,omitempty"`
	Extract     []ExtractRule `json:"extract,omitempty"`
	Checks      []Check       `json:"checks,omitempty"`
}

Request is a .request.jsonc file.

type Settings

type Settings struct {
	TimeoutMs       *int  `json:"timeoutMs,omitempty"`
	FollowRedirects *bool `json:"followRedirects,omitempty"`
	MaxRedirects    *int  `json:"maxRedirects,omitempty"`
	VerifyTLS       *bool `json:"verifyTls,omitempty"`
}

Settings are the per-request settings; a nil value means the default of the setting of the editor.

type Variable

type Variable struct {
	Name string
	// Value is the initial value; it is empty for a secret.
	Value string
	// Enabled is true unless the file says "enabled": false.
	Enabled bool
	// Secret says that the value is in the keyring.
	Secret bool
}

Variable is a variable of an environment or a collection. The value of a secret variable is never written to a file (D45).

func (Variable) MarshalJSON

func (v Variable) MarshalJSON() ([]byte, error)

MarshalJSON writes the variable: no "value" for a secret, "enabled" only when false, "secret" only when true.

func (*Variable) UnmarshalJSON

func (v *Variable) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a variable. A value in the file of a secret variable is dropped: it is not used and the next save removes it.

Jump to

Keyboard shortcuts

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