contract

package module
v0.0.0-...-da3cc26 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 3 Imported by: 10

README

Contract

Go Reference Go Report Card

Package contract provides interfaces and types for Gopi frame.

Installation

go get -u -v github.com/gopi-frame/contract

Import

import "github.com/gopi-frame/contract"

Interfaces

Arrayable
type Numbers struct {
	numbers []int
}

func (n *Numbers) ToArray() []int {
	return n.numbers
}
Comparator
type User struct {
	ID   int
	Name string
}

func (u *User) Compare(other *User) int {
	if u.ID > other.ID {
		return 1
	}
	if u.ID < other.ID {
		return -1
	}
	if u.Name > other.Name {
		return 1
	}
	if u.Name < other.Name {
		return -1
	}
	return 0
}
Countable
type Numbers struct {
	numbers []int
}

func (n *Numbers) Count() int64 {
	return int64(len(n.numbers))
}
Delayable
type Delay[int] struct {
	value int
	until time.Time
}

func (d *Delay[int]) Value() int { return d.value }

func (d *Delay[int]) Until() time.Time { return d.until }

func (d *Delay[int]) MarshalJSON(until time.Time) error {
    var jsonObject struct {
        Value int `json:"value"`
		Until time.Time `json:"until"`
	}
	jsonObject.Value = d.value
	jsonObject.Until = d.until
	return json.Marshal(jsonObject)
}

func (d *Delay[int]) UnmarshalJSON(b []byte) error {
	var jsonObject struct {
		Value int `json:"value"`
		Until time.Time `json:"until"`
    }
	if err := json.Unmarshal(b, &jsonObject); err!= nil {
		return err
	}
	d.value = jsonObject.Value
	d.until = jsonObject.Until
	return nil
}
Jsonable
type User struct {
	ID   int
	Name string
}

func (u *User) ToJSON() ([]byte, error) {
    return json.Marshal(u)
}
Mappable
type User struct {
	ID   int
    Name string
}

func (u *User) Map() map[string]interface{} {
	return map[string]interface{}{
		"id":   u.ID,
		"name": u.Name,
    }
}
List

See collection.List for more information.

Map

See collection.Map for more information.

Queue

See collection.Queue for more information.

BlockingQueue

See collection.Queue for more information.

DelayedQueue

See collection.Queue for more information.

Set

See collection.Set for more information.

Traversable
type Numbers struct {
	numbers []int
}

func (n *Numbers) Each(fn func(index int, value int) bool) {
	for i, v := range n.numbers {
		if !fn(i, v) {
			break
		}
	}
}
Sortable
type Numbers struct {
    numbers []int
}

func (n *Numbers) Sort(fn func(a, b int) int) {
	for i := 0; i < len(n.numbers); i++ {
		for j := i + 1; j < len(n.numbers); j++ {
			if fn(n.numbers[i], n.numbers[j]) > 0 {
				n.numbers[i], n.numbers[j] = n.numbers[j], n.numbers[i]
			}
		}
	}
}
Stringable
type User struct {
	ID   int
    Name string
}

func (u *User) String() string {
    return fmt.Sprintf("%v %v", u.ID, u.Name)
}
Reversible
type Numbers struct {
	numbers []int
}

func (n *Numbers) Reverse() {
	for i, j := 0, len(n.numbers)-1; i < j; i, j = i+1, j-1 {
		n.numbers[i], n.numbers[j] = n.numbers[j], n.numbers[i]
	}
}

Documentation

Overview

Package contract provides interfaces and types for Gopi frame.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arrayable

type Arrayable[E any] interface {
	// ToArray converts to array
	ToArray() []E
}

Arrayable arrayable

type BlockingQueue

type BlockingQueue[E any] interface {
	Queue[E]
	// TryEnqueue tries to enqueue element to the end of the queue
	TryEnqueue(E) bool
	// TryDequeue tries to dequeue the first element and return it
	TryDequeue() (E, bool)
	// EnqueueTimeout enqueues element to the end of the queue,
	// it will return false when timeout
	EnqueueTimeout(E, time.Duration) bool
	// DequeueTimeout dequeues the first element,
	// it will return zero value and false when timeout
	DequeueTimeout(time.Duration) (E, bool)
}

BlockingQueue blocking queue interface

type Comparator

type Comparator[T any] interface {
	Compare(a, b T) int
}

Comparator comparator

type Countable

type Countable interface {
	// Count returns the count
	Count() int64
}

Countable countable

type Delayable

type Delayable[T any] interface {
	json.Marshaler
	json.Unmarshaler
	// Until returns the available time
	Until() time.Time
	// Value returns the underlying value
	Value() T
}

Delayable delayable

type DelayedQueue

type DelayedQueue[E any] interface {
	Countable
	Arrayable[Delayable[E]]
	Jsonable
	json.Marshaler
	json.Unmarshaler
	Stringable
	IsEmpty() bool
	IsNotEmpty() bool
	Clear()
	Peek() (Delayable[E], bool)
	Enqueue(Delayable[E]) bool
	Dequeue() (Delayable[E], bool)
	TryDequeue() (Delayable[E], bool)
	DequeueTimeout(time.Duration) (Delayable[E], bool)
}

type Jsonable

type Jsonable interface {
	// ToJSON converts to json
	ToJSON() ([]byte, error)
}

Jsonable jsonable

type List

type List[E any] interface {
	Countable
	Sortable[E]
	Traversable[int, E]
	Reversible
	Stringable
	Jsonable
	Arrayable[E]
	json.Marshaler
	json.Unmarshaler
	// IsEmpty returns whether the list is empty.
	IsEmpty() bool
	// IsNotEmpty returns whether the list is not empty.
	IsNotEmpty() bool
	// Contains returns whether the list contains the specific element.
	Contains(E) bool
	// ContainsWhere returns whether the list contains specific elements by callback.
	ContainsWhere(func(E) bool) bool
	// Push pushes elements into the list.
	Push(...E)
	// Remove removes the specific element.
	Remove(E)
	// RemoveAt removes the element on the specific index.
	RemoveAt(int)
	// RemoveWhere removes specific elements by callback.
	RemoveWhere(func(E) bool)
	// Clear clears the list.
	Clear()
	// Get returns the element on the specific index.
	Get(int) E
	// Set sets element on the specific index.
	Set(int, E)
	// First returns the first element of the list.
	// it will return a zero value and false when the list is empty.
	First() (E, bool)
	// FirstOr returns the first element of the list, it will return the default value when the list is empty.
	FirstOr(E) E
	// FirstWhere returns the first element of the list which matches the callback.
	// It will return a zero value and false when none matches the callback.
	FirstWhere(func(E) bool) (E, bool)
	// FirstWhereOr returns the first element of the list which matches the callback.
	// It will return the default value when none matches the callback.
	FirstWhereOr(func(E) bool, E) E
	// Last returns the last element of the list.
	// It will return a zero value and false when the list is empty.
	Last() (E, bool)
	// LastOr returns the last element of the list.
	// It will return the default value when the list is empty.
	LastOr(E) E
	// LastWhere returns the last element of the list which matches the callback.
	// It will return a zero value and false when none matches the callback.
	LastWhere(func(E) bool) (E, bool)
	// LastWhereOr returns the last element of the list which matches the callback.
	// It will return the default value when none matches the callback.
	LastWhereOr(func(E) bool, E) E
	// Pop removes the last element of the list and returns it.
	// It will return a zero value and false when the list is empty.
	Pop() (E, bool)
	// Shift removes the first element of the list and returns it.
	// It will return a zero value and false when the list is empty.
	Shift() (E, bool)
	// Unshift puts elements to the head of the list.
	Unshift(...E)
	// IndexOf returns the index of the specific element.
	IndexOf(E) int
	// IndexOfWhere returns the index of the first element which matches the callback.
	IndexOfWhere(func(E) bool) int
	// Compact makes the list more compact
	Compact(eq func(E, E) bool)
	// Min returns the min element
	Min(func(E, E) int) E
	// Max returns the max element
	Max(func(E, E) int) E
}

List list

type Map

type Map[K comparable, V any] interface {
	Countable
	Traversable[K, V]
	Jsonable
	json.Marshaler
	json.Unmarshaler
	Mappable[K, V]
	Stringable
	// IsEmpty returns whether the map is empty.
	IsEmpty() bool
	// IsNotEmpty returns whether the map is not empty.
	IsNotEmpty() bool
	// Get returns the value of the specific key.
	// It will return a zero value and false when the map is empty.
	Get(K) (V, bool)
	// GetOr returns the value of the specific key.
	// It will return the default value when the map is empty.
	GetOr(K, V) V
	// Set sets value to specific key.
	Set(K, V)
	// Remove removes specific key.
	Remove(K)
	// Keys returns all keys.
	Keys() []K
	// Values returns all values.
	Values() []V
	// Clear clears map.
	Clear()
	// ContainsKey returns whether the map contains specific key.
	ContainsKey(K) bool
	// Contains returns whether the map contains specific value.
	Contains(V) bool
	// ContainsWhere returns whether the map contains value which matches the callback.
	ContainsWhere(func(V) bool) bool
}

Map map

type Mappable

type Mappable[K comparable, V any] interface {
	// ToMap converts to map
	ToMap() map[K]V
}

Mappable mappable

type Option

type Option[T any] interface {
	Apply(T) error
}

type Queue

type Queue[E any] interface {
	Countable
	Arrayable[E]
	Jsonable
	json.Marshaler
	json.Unmarshaler
	Stringable
	// IsEmpty returns whether the queue is empty
	IsEmpty() bool
	// IsNotEmpty returns whether the queue is not empty
	IsNotEmpty() bool
	// Clear clears the queue
	Clear()
	// Peek returns the first element of the queue.
	Peek() (E, bool)
	// Enqueue enqueues element to the end of the queue
	Enqueue(E) bool
	// Dequeue dequeues the first element
	Dequeue() (E, bool)
	// Remove removes the specific element
	Remove(E)
	// RemoveWhere removes the elements which matches the callback
	RemoveWhere(func(E) bool)
}

Queue queue interface

type Reversible

type Reversible interface {
	// Reverse reverse
	Reverse()
}

Reversible reversible

type Set

type Set[E comparable] interface {
	Countable
	Traversable[int, E]
	Arrayable[E]
	Jsonable
	json.Marshaler
	json.Unmarshaler
	Stringable
	// IsEmpty returns whether the set is empty
	IsEmpty() bool
	// IsNotEmpty returns whether the set is not empty
	IsNotEmpty() bool
	// Contains returns whether the set contains specific element
	Contains(E) bool
	// ContainsWhere returns whether the set contains element which matches the callback
	ContainsWhere(func(E) bool) bool
	// Push pushes elements to set
	Push(...E)
	// Remove removes the specific element
	Remove(E)
	// RemoveWhere removes elements which matches the callback
	RemoveWhere(func(E) bool)
	// Clear clears the set
	Clear()
}

Set sets

type Sortable

type Sortable[T any] interface {
	// Sort sorts
	Sort(func(T, T) int)
}

Sortable sortable

type Stringable

type Stringable = fmt.Stringer

Stringable is the alias of fmt.Stringer

type Traversable

type Traversable[K any, V any] interface {
	// Each travers, if the callback returns false then break
	Each(func(key K, value V) bool)
}

Traversable traversable

Directories

Path Synopsis
app module
cache module
console module
container module
database module
enum module
eventbus module
exception module
filesystem module
logger module
paginator module
pipeline module
queue module
redis module
repository module
response module
router module
server module
translator module
validation module
websocket module
writer module

Jump to

Keyboard shortcuts

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