rest

package
v0.0.0-...-e89bacc Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JsonParamsParseCode = "ERR_PARSE_PARAMS"
	JsonQueryParseCode  = "ERR_PARSE_QUERY"
	JsonBodyParseCode   = "ERR_PARSE_BODY"
	JsonValidateCode    = "ERR_VALIDATE_REQUEST"
	JsonValidateMessage = "Invalid request"
)
View Source
var (
	CanValidateType   = reflect.TypeOf((*CanValidate)(nil)).Elem()
	ValidateReflector = NewReflector(func(t reflect.Type) bool {
		return t.Implements(CanValidateType)
	})
)
View Source
var DifficultyMap = map[string]su.ClearLimit{
	"":           su.DifficultyMedium,
	"beginner":   su.DifficultyBeginner,
	"easy":       su.DifficultyEasy,
	"medium":     su.DifficultyMedium,
	"hard":       su.DifficultyHard,
	"tricky":     su.DifficultyTricky,
	"diabolical": su.DifficultyDiabolical,
	"fiendish":   su.DifficultyFiendish,
	"custom":     customClearLimit,
}

Functions

func GetRouter

func GetRouter() chi.Router

func JsonHandle

func JsonHandle(handle func(w http.ResponseWriter, r *http.Request) (any, int)) http.HandlerFunc

func JsonRoute

func JsonRoute[B any, P any, Q any](handler JsonHandler[B, P, Q]) http.HandlerFunc

func ParamsToMap

func ParamsToMap(r *http.Request) any

func ParseBody

func ParseBody[T any](r *http.Request) (T, error)

func ParseMap

func ParseMap[T any](m any) (T, error)

func QueryToMap

func QueryToMap(r *http.Request) any

func Start

func Start(addr string) error

Types

type CanValidate

type CanValidate interface {
	Validate(v Validator)
}

type ConstraintDifference

type ConstraintDifference struct {
	Min      int                 `json:"min"`
	Max      int                 `json:"max"`
	Cells    *[]Position         `json:"cells"`
	Relative *[]RelativePosition `json:"relative"`
	Exclude  *[]Position         `json:"exclude"`
}

type ConstraintDivisible

type ConstraintDivisible struct {
	By        int        `json:"by"`
	Remainder int        `json:"remainder"`
	Cells     []Position `json:"cells"`
}

type ConstraintMagic

type ConstraintMagic struct {
	Center Position `json:"center"`
}

type ConstraintOrder

type ConstraintOrder struct {
	Cells     *[]Position         `json:"cells"`
	Relative  *[]RelativePosition `json:"relative"`
	Direction DirectionInt        `json:"direction"`
}

type ConstraintScalePair

type ConstraintScalePair struct {
	Scale  int      `json:"scale"`
	First  Position `json:"first"`
	Second Position `json:"second"`
}

type ConstraintSumCell

type ConstraintSumCell struct {
	Sum         RelativePosition    `json:"sum"`
	SumRelative Trim[bool]          `json:"sumRelative"`
	Cells       *[]Position         `json:"cells"`
	Relative    *[]RelativePosition `json:"relative"`
}

type ConstraintSumCells

type ConstraintSumCells struct {
	Sum         []RelativePosition  `json:"sum"`
	SumRelative Trim[bool]          `json:"sumRelative"`
	Cells       *[]Position         `json:"cells"`
	Relative    *[]RelativePosition `json:"relative"`
}

type ConstraintSumValue

type ConstraintSumValue struct {
	Sum      int                 `json:"sum"`
	Cells    *[]Position         `json:"cells"`
	Relative *[]RelativePosition `json:"relative"`
}

func (ConstraintSumValue) Validate

func (c ConstraintSumValue) Validate(v Validator)

type ConstraintUnique

type ConstraintUnique struct {
	Cells    *[]Position         `json:"cells"`
	Relative *[]RelativePosition `json:"relative"`
	Same     Trim[bool]          `json:"same"`
}

type Constraints

type Constraints struct {
	SumValues   []ConstraintSumValue   `json:"sumValues"`
	SumCell     []ConstraintSumCell    `json:"sumCell"`
	SumCells    []ConstraintSumCells   `json:"sumCells"`
	Uniques     []ConstraintUnique     `json:"uniques"`
	Orders      []ConstraintOrder      `json:"orders"`
	Magics      []ConstraintMagic      `json:"magics"`
	ScalePairs  []ConstraintScalePair  `json:"scalePairs"`
	Differences []ConstraintDifference `json:"differences"`
	Divisibles  []ConstraintDivisible  `json:"divisibles"`
}

type DirectionInt

type DirectionInt int

func (*DirectionInt) Validate

func (d *DirectionInt) Validate(v Validator)

type Format

type Format string
var (
	FormatText Format = "text"
	FormatPDF  Format = "pdf"
	FormatJson Format = "json"
)

type FormatParam

type FormatParam struct {
	Format Format `json:"format"`
}

func (*FormatParam) Validate

func (f *FormatParam) Validate(v Validator)

type GenerateCount

type GenerateCount int

func (*GenerateCount) Validate

func (g *GenerateCount) Validate(v Validator)

type GenerateFormatManyJson

type GenerateFormatManyJson struct {
	Seed    int64             `json:"seed"`
	Kind    GenerateKind      `json:"kind"`
	Puzzles []GeneratedPuzzle `json:"puzzles"`
}

type GenerateFormatManyQuery

type GenerateFormatManyQuery struct {
	PDF OptionsPDF `json:"pdf"`
}

type GenerateFormatSingleJson

type GenerateFormatSingleJson struct {
	Seed    int64             `json:"seed"`
	Puzzles []GeneratedPuzzle `json:"puzzles"`
}

type GenerateFormatSingleQuery

type GenerateFormatSingleQuery struct {
	GenerateKind
	PDF OptionsPDF `json:"pdf"`
}

type GenerateKind

type GenerateKind struct {
	Count          Trim[GenerateCount]   `json:"count"`
	Seed           Trim[GenerateSeed]    `json:"seed"`
	Difficulty     string                `json:"difficulty"`
	MinCost        Trim[int]             `json:"minCost"`
	MaxCost        Trim[int]             `json:"maxCost"`
	MaxPlacements  Trim[int]             `json:"maxPlacements"`
	MaxSteps       Trim[int]             `json:"maxSteps"`
	MaxBatches     Trim[int]             `json:"maxBatches"`
	Symmetric      Trim[bool]            `json:"symmetric"`
	BoxWidth       Trim[PuzzleDimension] `json:"boxWidth"`
	BoxHeight      Trim[PuzzleDimension] `json:"boxHeight"`
	Techniques     map[string]Trim[int]  `json:"techniques"`
	Constraints    Constraints           `json:"constraints"`
	Candidates     Trim[bool]            `json:"candidates"`
	State          Trim[bool]            `json:"state"`
	Solutions      Trim[bool]            `json:"solutions"`
	SolutionSteps  Trim[bool]            `json:"solutionSteps"`
	SolutionStates Trim[bool]            `json:"solutionStates"`
	TryCount       Trim[int]             `json:"tryCount"`
	TryAttempts    Trim[int]             `json:"tryAttempts"`
	TryClears      Trim[int]             `json:"tryClears"`
}

func (*GenerateKind) Validate

func (r *GenerateKind) Validate(v Validator)

type GenerateRequest

type GenerateRequest struct {
	Seed  GenerateSeed   `json:"seed"`
	Kinds []GenerateKind `json:"kinds"`
}

func (GenerateRequest) Validate

func (r GenerateRequest) Validate(v Validator)

type GenerateSeed

type GenerateSeed int64

func (*GenerateSeed) Validate

func (g *GenerateSeed) Validate(v Validator)

type GeneratedPuzzle

type GeneratedPuzzle struct {
	Difficulty string            `json:"difficulty,omitempty"`
	Puzzle     PuzzleData        `json:"puzzle"`
	Duration   string            `json:"duration"`
	Solution   *PuzzleData       `json:"solution,omitempty"`
	Steps      []PuzzleSolveStep `json:"steps,omitempty"`

	Solver *su.Solver `json:"-"`
}

type IDParam

type IDParam struct {
	ID string `json:"id"`
}

func (IDParam) Validate

func (p IDParam) Validate(v Validator)

type Index

type Index int

func (*Index) Validate

func (d *Index) Validate(v Validator)

type JsonError

type JsonError struct {
	Message     string       `json:"message"`
	Code        string       `json:"code"`
	Validations []Validation `json:"validations,omitempty"`
}

func JsonErrorFromParse

func JsonErrorFromParse(e error, code string) JsonError

func JsonErrorFromValidations

func JsonErrorFromValidations(message string, code string, validations []Validation) JsonError

type JsonHandler

type JsonHandler[B any, P any, Q any] func(r JsonRequest[B, P, Q]) (any, int)

type JsonRequest

type JsonRequest[B any, P any, Q any] struct {
	Body     B
	Params   P
	Query    Q
	Request  *http.Request
	Response http.ResponseWriter
	Validate ValidateContext
}

func (JsonRequest[B, P, Q]) SendJson

func (r JsonRequest[B, P, Q]) SendJson(data any, status int) (any, int)

func (JsonRequest[B, P, Q]) SendText

func (r JsonRequest[B, P, Q]) SendText(text string, status int) (any, int)

func (JsonRequest[B, P, Q]) URL

func (r JsonRequest[B, P, Q]) URL() url.URL

type None

type None struct{}

type Optional

type Optional[T any] struct {
	Value   T
	Defined bool
}

func (Optional[T]) MarshalJSON

func (o Optional[T]) MarshalJSON() ([]byte, error)

func (*Optional[T]) Set

func (o *Optional[T]) Set(value T)

func (*Optional[T]) UnmarshalJSON

func (o *Optional[T]) UnmarshalJSON(data []byte) error

func (*Optional[T]) Unset

func (o *Optional[T]) Unset()

type OptionsPDF

type OptionsPDF struct {
	PuzzlesWide Trim[int] `json:"puzzlesWide"`
	PuzzlesHigh Trim[int] `json:"puzzlesHigh"`
}

func (*OptionsPDF) Validate

func (o *OptionsPDF) Validate(v Validator)

type Position

type Position struct {
	Col Index `json:"col"`
	Row Index `json:"row"`
}

type PuzzleData

type PuzzleData struct {
	BoxWidth   int       `json:"boxWidth,omitempty"`
	BoxHeight  int       `json:"boxHeight,omitempty"`
	Values     [][]int   `json:"values"`
	Candidates [][][]int `json:"candidates,omitempty"`
	Encoded    string    `json:"encoded,omitempty"`
	State      string    `json:"state,omitempty"`

	Puzzle *su.Puzzle `json:"-"`
}

type PuzzleDimension

type PuzzleDimension int

func (*PuzzleDimension) Validate

func (d *PuzzleDimension) Validate(v Validator)

type PuzzleFormatSingleJson

type PuzzleFormatSingleJson struct {
	BoxWidth   int       `json:"boxWidth"`
	BoxHeight  int       `json:"boxHeight"`
	Puzzle     [][]int   `json:"puzzle"`
	Candidates [][][]int `json:"candidates,omitempty"`
}

type PuzzleFormatSingleQuery

type PuzzleFormatSingleQuery struct {
	Candidates Trim[bool] `json:"candidates"`
	State      Trim[bool] `json:"state"`
	Solution   Trim[bool] `json:"solution"`
}

type PuzzleParams

type PuzzleParams struct {
	FormatParam
	IDParam
}

type PuzzleSolveStep

type PuzzleSolveStep struct {
	Technique         string      `json:"technique"`
	Index             int         `json:"index"`
	Batch             int         `json:"batch"`
	Cost              int         `json:"cost"`
	Placement         bool        `json:"placement"`
	Row               int         `json:"row"`
	Col               int         `json:"col"`
	Before            int         `json:"before"`
	BeforeCandidates  []int       `json:"beforeCandidates"`
	After             int         `json:"after"`
	AfterCandidates   []int       `json:"afterCandidates"`
	RunningCost       int         `json:"runningCost"`
	RunningPlacements int         `json:"runningPlacements"`
	State             *PuzzleData `json:"state,omitempty"`

	Log su.SolverLog `json:"-"`
}

type ReflectConsumer

type ReflectConsumer interface {
	Consume(v reflect.Value)
	ForIndex(index int) ReflectConsumer
	ForField(field reflect.StructField) ReflectConsumer
	ForKey(key string) ReflectConsumer
}

type ReflectIterator

type ReflectIterator func(value reflect.Value, consumer ReflectConsumer)

type ReflectShouldConsume

type ReflectShouldConsume func(t reflect.Type) bool

type Reflector

type Reflector struct {
	Iterators     map[reflect.Type][]ReflectIterator
	ShouldConsume ReflectShouldConsume
}

func NewReflector

func NewReflector(should ReflectShouldConsume) *Reflector

func (*Reflector) Consume

func (r *Reflector) Consume(value any, consumer ReflectConsumer)

type RelativeIndex

type RelativeIndex int

func (*RelativeIndex) Validate

func (d *RelativeIndex) Validate(v Validator)

type RelativePosition

type RelativePosition struct {
	Col RelativeIndex `json:"col"`
	Row RelativeIndex `json:"row"`
}

type SolutionsFormatComplexJson

type SolutionsFormatComplexJson struct {
	BoxWidth  int                              `json:"boxWidth"`
	BoxHeight int                              `json:"boxHeight"`
	Puzzle    [][]int                          `json:"puzzle"`
	Duration  string                           `json:"duration"`
	Solutions []SolutionsFormatComplexSolution `json:"solutions"`
}

type SolutionsFormatComplexQuery

type SolutionsFormatComplexQuery struct {
	Steps  Trim[bool] `json:"steps"`
	States Trim[bool] `json:"stepStates"`
	Limit  Trim[int]  `json:"limit"`
}

func (*SolutionsFormatComplexQuery) Validate

func (q *SolutionsFormatComplexQuery) Validate(v Validator)

type SolutionsFormatComplexSolution

type SolutionsFormatComplexSolution struct {
	Solution [][]int           `json:"solution"`
	Cost     int               `json:"cost"`
	Steps    []PuzzleSolveStep `json:"steps,omitempty"`
}

type SolveFormatComplexJson

type SolveFormatComplexJson struct {
	BoxWidth  int               `json:"boxWidth"`
	BoxHeight int               `json:"boxHeight"`
	Puzzle    [][]int           `json:"puzzle"`
	Solution  [][]int           `json:"solution"`
	Unique    bool              `json:"unique"`
	Duration  string            `json:"duration"`
	Cost      int               `json:"cost"`
	Steps     []PuzzleSolveStep `json:"steps,omitempty"`
}

type SolveFormatComplexQuery

type SolveFormatComplexQuery struct {
	Steps      Trim[bool] `json:"steps"`
	States     Trim[bool] `json:"stepStates"`
	Candidates Trim[bool] `json:"stepCandidates"`
}

type SolveFormatSingleJson

type SolveFormatSingleJson struct {
	BoxWidth  int               `json:"boxWidth"`
	BoxHeight int               `json:"boxHeight"`
	Puzzle    [][]int           `json:"puzzle"`
	Solution  [][]int           `json:"solution"`
	Unique    bool              `json:"unique"`
	Duration  string            `json:"duration"`
	Cost      int               `json:"cost"`
	Steps     []PuzzleSolveStep `json:"steps,omitempty"`
}

type SolveFormatSingleQuery

type SolveFormatSingleQuery struct {
	Steps      Trim[bool] `json:"steps"`
	States     Trim[bool] `json:"stepStates"`
	Candidates Trim[bool] `json:"stepCandidates"`
}

type SolveKind

type SolveKind struct {
	BoxWidth       PuzzleDimension `json:"boxWidth"`
	BoxHeight      PuzzleDimension `json:"boxHeight"`
	Puzzle         [][]int         `json:"puzzle"`
	MinCost        int             `json:"minCost"`
	MaxCost        int             `json:"maxCost"`
	MaxPlacements  int             `json:"maxPlacements"`
	MaxSteps       int             `json:"maxSteps"`
	MaxBatches     int             `json:"maxBatches"`
	Techniques     map[string]int  `json:"techniques"`
	Constraints    Constraints     `json:"constraints"`
	Candidates     bool            `json:"candidates"`
	SolutionSteps  bool            `json:"solutionSteps"`
	SolutionStates bool            `json:"solutionStates"`
}

func (*SolveKind) Validate

func (r *SolveKind) Validate(v Validator)

type Trim

type Trim[T any] struct {
	Value T `validate:"-"`
}

func (*Trim[T]) UnmarshalJSON

func (t *Trim[T]) UnmarshalJSON(data []byte) error

type ValidateContext

type ValidateContext map[string]any

type Validation

type Validation struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

type Validator

type Validator struct {
	Path        []string
	Validations *[]Validation
	Context     ValidateContext
}

func NewValidator

func NewValidator() Validator

func (*Validator) Add

func (v *Validator) Add(format string, args ...any)

func (*Validator) AddField

func (v *Validator) AddField(field string, format string, args ...any)

func (Validator) Consume

func (v Validator) Consume(rv reflect.Value)

func (Validator) Field

func (v Validator) Field(field string) Validator

func (Validator) ForField

func (v Validator) ForField(field reflect.StructField) ReflectConsumer

func (Validator) ForIndex

func (v Validator) ForIndex(index int) ReflectConsumer

func (Validator) ForKey

func (v Validator) ForKey(key string) ReflectConsumer

func (Validator) IsValid

func (v Validator) IsValid() bool

func (Validator) Validate

func (v Validator) Validate(value any)

Jump to

Keyboard shortcuts

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