apidoc

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: BSD-2-Clause Imports: 13 Imported by: 0

README

gadventures/apidoc

Go Report Card Go Reference

WARNING: alpha quality code; use at your own risk.

Generic <rest.gadventures.com> G API Document datatype

It extracts the common code for dealing with resource (JSON) "documents" from the G API.

Features include

  • Calculating checksums (ETag)
  • Specific behaviour on how the G API de/serializes JSON (e.g. nil slices are [], not null)
  • Equal method to be able to compare one Document to another
  • Binary serialization, which is faster than JSON and thanks to golang/snappy, results in smaller payload size
  • Evaluating if the response is a GAPIError

The core datatype Document, is an alias for map[string]interface{}. While this will be slower than using custom structs, the tradeoff is that Document instances do not require customization based on the requested resource. In other words adding/removing attributes from G API resources does not require modifications to the gadventures/apidoc module.

Usage

specify require github.com/gadventures/apidoc v0.2.0 in your go.mod file

package main

import (
	"encoding/json"
	"log"

	"github.com/gadventures/apidoc"
)

const blob := `{
    "street": "Stand 1385",
    "city": "Victoria Falls",
    "country": {
        "id": "ZW",
        "href": "https://rest.gadventures.com/countries/ZW",
        "name": "Zimbabwe"
    }
}`

func main() {
	doc = apidoc.New()
	err := json.Unmarshal([]byte(blob), &doc)
	if err != nil {
		log.Fatal(err.Error())
	}
	
	fmt.Println(doc["city"].(string)) // will print "Victoria Falls"
	
	id, ok := doc.GetPath("country", "id")
	if !ok {
		log.Fatal("could not find country.id in document")
	}
	fmt.Println(countryID.(string)) // will print "ZW"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document map[string]interface{}

Document represents a single G API resource. Internally its contents would be represented by Go primitives.

bool for booleans float64 for numbers string for strings []interface{} for arrays Document for nested objects nil for nulls

func New

func New() Document

New returns new Document

func (*Document) Copy

func (d *Document) Copy() *Document

Copy creates a new copy of document

func (Document) ETag

func (d Document) ETag() (ETag, error)

ETag returns the checksum of the document

func (Document) Equal

func (d Document) Equal(other Document) bool

Equal compares if two Documents are the same

func (Document) GAPIError

func (d Document) GAPIError(uri string) *ErrGAPI

GAPIError returns *ErrGAPI if the document is a GAPI error, nil otherwise

func (Document) GetPath

func (d Document) GetPath(parts ...string) (interface{}, bool)

GetPath recursively searches for value at provided path

e.g. GetPath("staff_profile", "id") would return "value" under the

    '/staff_profile/id' path in the Document:

{
  "staff_profile": {
    "id": "value"
  }
}

func (Document) Keys added in v0.2.0

func (d Document) Keys() []string

Keys is a convenience method to return the keys in this Document

func (Document) KeysSorted added in v0.2.0

func (d Document) KeysSorted() []string

KeysSorted is a convenience method to return the keys in this Document, in sorted order

func (Document) MarshalBinary

func (d Document) MarshalBinary() ([]byte, error)

MarshalBinary allows documents to be stored in cache

func (Document) MarshalJSON

func (d Document) MarshalJSON() ([]byte, error)

MarshalJSON implements json marshaling of Document

func (Document) String

func (d Document) String() string

String satisifies the Stringer interface

func (Document) TraverseCall

func (d Document) TraverseCall(f TraverseFunc)

TraverseCall will visit every item in Document and call the provided TraverseFunc on each item

func (*Document) UnmarshalBinary

func (d *Document) UnmarshalBinary(data []byte) error

UnmarshalBinary implements binary decoding

func (*Document) UnmarshalJSON

func (d *Document) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json unmarshaling of Document

func (Document) WriteOutJSON

func (d Document) WriteOutJSON(w io.Writer) error

WriteOutJSON is more memory efficient alternative to MarshalJSON it so because it directly writes to io.Writer without a need to alocate another buffer

type ETag

type ETag uint64

ETag is type for storing the calculated etag/checksum of a Document

func NewETag

func NewETag(s string) (ETag, error)

NewETag returns new ETag initialized with provided hexadecimal string

func (ETag) String

func (e ETag) String() string

type ErrGAPI

type ErrGAPI struct {
	HTTPStatusCode int
	Message        string
	ErrorID        string
	URI            string
}

ErrGAPI type represents error response returned by GAPI

See: https://developers.gadventures.com/docs/rest.html#errors

func (*ErrGAPI) Error

func (r *ErrGAPI) Error() string

Error satisfies the error interface for the ErrGAPI type

type TraverseFunc

type TraverseFunc = func(*Document, string, interface{})

TraverseFunc is an alias for a function that will be executed for every item in the Document

Jump to

Keyboard shortcuts

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