Documentation ¶
Index ¶
- Variables
- func Parse(data []byte) (interface{}, error)
- type Calculable
- type Document
- func (d *Document) Digest() (*dsig.Digest, error)
- func (d *Document) Instance() interface{}
- func (d *Document) IsEmpty() bool
- func (Document) JSONSchema() *jsonschema.Schema
- func (d *Document) MarshalJSON() ([]byte, error)
- func (d *Document) Schema() schema.ID
- func (d *Document) UnmarshalJSON(data []byte) error
- func (d *Document) Validate() error
- type Envelope
- type Error
- type Header
- type Signatures
- type Version
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRegion is used when the envelope is missing a region. ErrNoRegion = NewError("no-region") // ErrNoDocument is provided when the envelope does not contain a // document payload. ErrNoDocument = NewError("no-document") // ErrValidation is used when a document fails a validation request. ErrValidation = NewError("validation") // ErrCalculation wraps around errors that we're generated during a // call to perform calculations on a document. ErrCalculation = NewError("calculation") // ErrMarshal is provided when there has been a problem attempting to encode // or marshal an object, usually into JSON. ErrMarshal = NewError("marshal") // ErrUnmarshal is used when that has been a problem attempting to read the // source data. ErrUnmarshal = NewError("unmarshal") // ErrDraft is raised when attempting to perform an operation that can only // be performed on an envelope that is not a draft. ErrDraft = NewError("draft") // ErrSignature identifies an issue related to signatures. ErrSignature = NewError("signature") // ErrInternal is a "catch-all" for errors that are not expected. ErrInternal = NewError("internal") // ErrUnknownSchema is provided when we attempt to determine the schema for an object // or from an ID and cannot find a match. ErrUnknownSchema = NewError("unknown-schema") )
var EnvelopeSchema = schema.GOBL.Add("envelope")
EnvelopeSchema sets the general definition of the schema ID for this version of the envelope.
Functions ¶
Types ¶
type Calculable ¶
type Calculable interface {
Calculate() error
}
Calculable defines the methods expected of a document payload that contains a `Calculate` method to be used to perform any additional calculations.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document helps us handle the document's contents by essentially wrapping around the contents and ensuring that a `$schema` property is added automatically when marshalling into JSON.
func NewDocument ¶ added in v0.18.0
NewDocument instantiates a Document wrapper around the provided object.
func (*Document) Digest ¶ added in v0.17.0
Digest calculates a digital digest using the canonical JSON of the document.
func (*Document) Instance ¶ added in v0.18.0
func (d *Document) Instance() interface{}
Instance returns a prepared version of the document's content.
func (Document) JSONSchema ¶ added in v0.17.0
func (Document) JSONSchema() *jsonschema.Schema
JSONSchema returns a jsonschema.Schema instance.
func (*Document) MarshalJSON ¶ added in v0.17.0
MarshalJSON satisfies the json.Marshaler interface.
func (*Document) UnmarshalJSON ¶ added in v0.17.0
UnmarshalJSON satisfies the json.Unmarshaler interface.
type Envelope ¶
type Envelope struct { // Schema identifies the schema that should be used to understand this document Schema schema.ID `json:"$schema" jsonschema:"title=JSON Schema ID"` // Details on what the contents are Head *Header `json:"head" jsonschema:"title=Header"` // The data inside the envelope Document *Document `json:"doc" jsonschema:"title=Document"` // JSON Web Signatures of the header Signatures []*dsig.Signature `json:"sigs,omitempty" jsonschema:"title=Signatures"` }
Envelope wraps around a gobl document and provides support for digest creation and digital signatures.
func Envelop ¶ added in v0.20.0
Envelop is a convenience method that will build a new envelope and insert the contents document provided in a single swoop. The resulting envelope will still need to be signed afterwards.
func NewEnvelope ¶
func NewEnvelope() *Envelope
NewEnvelope builds a new envelope object ready for data to be inserted and signed. If you are loading data from json, you can safely use a regular `new(Envelope)` call directly.
Example (Complete) ¶
package main import ( "encoding/json" "fmt" "github.com/invopop/gobl" "github.com/invopop/gobl/note" "github.com/invopop/gobl/uuid" ) func main() { // Prepare a new Envelope with a region env := gobl.NewEnvelope() env.Head.UUID = uuid.MustParse("871c1e6a-8b5c-11ec-af5f-3e7e00ce5635") // Prepare a payload and insert msg := ¬e.Message{ Content: "sample message content", } if err := env.Insert(msg); err != nil { panic(err.Error()) } if err := env.Validate(); err != nil { panic(err.Error()) } data, err := json.MarshalIndent(env, "", "\t") if err != nil { panic(err.Error()) } fmt.Printf("%v\n", string(data)) }
Output: { "$schema": "https://gobl.org/draft-0/envelope", "head": { "uuid": "871c1e6a-8b5c-11ec-af5f-3e7e00ce5635", "dig": { "alg": "sha256", "val": "7d539c46ca03a4ecb1fcc4cb00d2ada34275708ee326caafee04d9dcfed862ee" } }, "doc": { "$schema": "https://gobl.org/draft-0/note/message", "content": "sample message content" } }
func (*Envelope) Calculate ¶ added in v0.28.0
Calculate is used to perform calculations on the envelope's document contents to ensure everything looks correct. Headers will be refreshed to ensure they have the latest valid digest.
func (*Envelope) Extract ¶
func (e *Envelope) Extract() interface{}
Extract the contents of the envelope into the provided document type.
func (*Envelope) Insert ¶
Insert takes the provided document and inserts it into this envelope. Calculate will be called automatically.
type Error ¶
Error provides a structure to better be able to make error comparisons. The contents can also be serialised as JSON ready to send to a client if needed.
func (*Error) Is ¶
Is checks to see if the target error matches the current error or part of the chain.
func (*Error) WithErrorf ¶
WithErrorf wraps around the `fmt.Errorf` call to provide a more meaningful error in the context.
type Header ¶
type Header struct { // Unique UUIDv1 identifier for the envelope. UUID uuid.UUID `json:"uuid" jsonschema:"title=UUID"` // Digest of the canonical JSON body. Digest *dsig.Digest `json:"dig" jsonschema:"title=Digest"` // Seals of approval from other organisations. Stamps []*cbc.Stamp `json:"stamps,omitempty" jsonschema:"title=Stamps"` // Set of labels that describe but have no influence on the data. Tags []string `json:"tags,omitempty" jsonschema:"title=Tags"` // Additional semi-structured information about this envelope. Meta cbc.Meta `json:"meta,omitempty" jsonschema:"title=Meta"` // Any information that may be relevant to other humans about this envelope Notes string `json:"notes,omitempty" jsonschema:"title=Notes"` // When true, implies that this document should not be considered final. Digital signatures are optional. Draft bool `json:"draft,omitempty" jsonschema:"title=Draft"` }
Header defines the meta data of the body. The header is used as the payload for the JSON Web Signatures, so we want this to be as compact as possible.
type Signatures ¶
Signatures keeps together a list of signatures that we're used to sign the document head contents.