data

package
v0.0.0-...-fc0cb9f Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package classification of Product API

Documentation for Product API

Schemes: http
BasePath: /
Version: 1.0.0

Consumes:
- application/json

Produces:
- application/json

swagger:meta

Index

Constants

This section is empty.

Variables

View Source
var ErrProductNotFound = fmt.Errorf("Product not found")

Functions

func AddProduct

func AddProduct(p *Product)

func DeleteProduct

func DeleteProduct(id int) error

DeleteProduct deletes a product from the database

func FromJSON

func FromJSON(i interface{}, r io.Reader) error

FromJSON deserializes the object from JSON string in an io.Reader to the given interface

func ToJSON

func ToJSON(i interface{}, w io.Writer) error

ToJSON serializes the given interface into a string based JSON format

func UpdateProduct

func UpdateProduct(p Product) error

UpdateProduct replaces a product in the database with the given item. If a product with the given id does not exist in the database this function returns a ProductNotFound error

Types

type Product

type Product struct {
	// the id for this user
	//
	// required: true
	// min: 1
	ID int `json:"id"`

	// the name for this poduct
	//
	// required: true
	// max length: 255
	Name string `json:"name" validate:"required"`

	// the description for this poduct
	//
	// required: false
	// max length: 10000
	Description string `json:"description"`

	// the price for the product
	//
	// required: true
	// min: 0.01
	Price float32 `json:"price" validate:"gt=0"`

	// the SKU for the product
	//
	// required: true
	// pattern: [a-z]+-[a-z]+-[a-z]+
	SKU string `json:"sku" validate:"required,sku"`
}

Product defines the structure for an API product swagger:model

func FindProduct

func FindProduct(id int) (*Product, int, error)

func GetProductByID

func GetProductByID(id int) (*Product, error)

GetProductByID returns a single product which matches the id from the database. If a product is not found this function returns a ProductNotFound error

func (*Product) FromJSON

func (p *Product) FromJSON(r io.Reader) error

type Products

type Products []*Product

Products is a collection of Product

func GetProducts

func GetProducts() Products

GetProducts returns a list of products

func (*Products) ToJSON

func (p *Products) ToJSON(w io.Writer) error

ToJSON serializes the contents of the collection to JSON NewEncoder provides better performance than json.Unmarshal as it does not have to buffer the output into an in memory slice of bytes this reduces allocations and the overheads of the service

https://golang.org/pkg/encoding/json/#NewEncoder

type Validation

type Validation struct {
	// contains filtered or unexported fields
}

Validation contains

func NewValidation

func NewValidation() *Validation

NewValidation creates a new Validation type

func (*Validation) Validate

func (v *Validation) Validate(i interface{}) ValidationErrors

Validate the item for more detail the returned error can be cast into a validator.ValidationErrors collection

if ve, ok := err.(validator.ValidationErrors); ok {
			fmt.Println(ve.Namespace())
			fmt.Println(ve.Field())
			fmt.Println(ve.StructNamespace())
			fmt.Println(ve.StructField())
			fmt.Println(ve.Tag())
			fmt.Println(ve.ActualTag())
			fmt.Println(ve.Kind())
			fmt.Println(ve.Type())
			fmt.Println(ve.Value())
			fmt.Println(ve.Param())
			fmt.Println()
	}

type ValidationError

type ValidationError struct {
	validator.FieldError
}

ValidationError wraps the validators FieldError so we do not expose this to out code

func (ValidationError) Error

func (v ValidationError) Error() string

type ValidationErrors

type ValidationErrors []ValidationError

ValidationErrors is a collection of ValidationError

func (ValidationErrors) Errors

func (v ValidationErrors) Errors() []string

Errors converts the slice into a string slice

Jump to

Keyboard shortcuts

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