hourstable

package module
v0.0.0-...-2ecc19b Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

hourstable data type

Build Status Go Report Card GoDoc Coverage Status

Implementation of the hour-table for a week (represents active hours by days of the week). It could be used as the SQL compatible data-type ad stored as String or JSON type in relational or document-oriented DB like PostgreSQL, MySQL, Oracle, MongoDB, etc.

Hourls-table example

Day of week / Hour 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Monday X X X X X
Tuesday X X X X X X X X X X X
Wednesday X X X X X X X X X X X
Thursday X X X X X X X X X X X
Friday X X X X X X X X X X X
Saturday X X X X X
Sunday X X X X X

In a database, it could be stored as TEXT of '1' and '0' symbols

111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111

or as the JSON object

{
  "mon": "111111111111111111111111",
  "tue": "111111111111111111111111",
  "wed": "000000000000000000000000",
  "thu": "000000000000000000000000",
  "fri": "000000000000000000000000",
  "sat": "111111111111111111111111",
  "sun": "000000001111111111111111"
}

or short form of JSON

{
  "mon": "*",
  "tue": "*",
  "sat": "*",
  "sun": "000000001111111111111111"
}

Testing & benchmarks

go test -timeout 30s github.com/demdxx/hourstable -v -race
Benchmarks
go test -benchmem -run=^$ github.com/demdxx/hourstable -bench . -v -race

pkg: github.com/demdxx/hourstable
Benchmark_Hours-8   	 1000000	      1776 ns/op	       0 B/op	       0 allocs/op
PASS
coverage: 8.3% of statements
ok  	github.com/demdxx/hourstable	2.826s
Success: Benchmarks passed.

Documentation

Index

Constants

View Source
const (
	ActiveDayHoursString   = "111111111111111111111111"
	DisabledDayHoursString = "000000000000000000000000"
	AllActiveHoursString   = "*" // default value to save the space
	ActiveWeekHoursString  = "" +
		ActiveDayHoursString +
		ActiveDayHoursString +
		ActiveDayHoursString +
		ActiveDayHoursString +
		ActiveDayHoursString +
		ActiveDayHoursString +
		ActiveDayHoursString // Saturday
)

Default constants...

Variables

View Source
var ErrTooMuchHoursForDecode = errors.New("[hours] too much hours for decode, mpre then 24*7")

ErrTooMuchHoursForDecode tells that hours more then for a week

Functions

func ActiveHoursRangeString

func ActiveHoursRangeString(from, to byte) string

ActiveHoursRangeString returns preformatted string with marked active houts according to range

Types

type Hours

type Hours []byte

Hours type

func HoursByJSON

func HoursByJSON(data []byte) (Hours, error)

HoursByJSON decodes JSON format of timetable

func HoursByString

func HoursByString(s string) (h Hours, err error)

HoursByString returns hours value or error

func MustHoursByString

func MustHoursByString(s string) Hours

MustHoursByString returns hours value or panic

func (Hours) Equal

func (h Hours) Equal(h2 Hours) bool

Equal comarison of two hour tables

func (Hours) IsAllActive

func (h Hours) IsAllActive() bool

IsAllActive then return the true

func (Hours) IsNoActive

func (h Hours) IsNoActive() bool

IsNoActive then return the true

func (Hours) MarshalJSON

func (h Hours) MarshalJSON() ([]byte, error)

MarshalJSON implements the functionality of json.Marshaler interface

func (Hours) Merge

func (h Hours) Merge(h2 Hours)

Merge from another hours

func (*Hours) Scan

func (h *Hours) Scan(value interface{}) (err error)

Scan - Implement the database/sql scanner interface

func (*Hours) SetHour

func (h *Hours) SetHour(weekDay time.Weekday, hour byte, active bool)

SetHour as active or no

func (Hours) String

func (h Hours) String() string

String implementation of fmt.Stringer

func (Hours) TestHour

func (h Hours) TestHour(weekDay time.Weekday, hour byte) bool

TestHour hour

func (Hours) TestTime

func (h Hours) TestTime(t time.Time) bool

TestTime hour

func (*Hours) UnmarshalJSON

func (h *Hours) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the functionality of json.Unmarshaler interface

func (Hours) Value

func (h Hours) Value() (driver.Value, error)

Value implementation of valuer for database/sql

type HoursJSON

type HoursJSON Hours

HoursJSON supports the JSON format of storing

func (HoursJSON) Equal

func (h HoursJSON) Equal(h2 Hours) bool

Equal comarison of two hour tables

func (HoursJSON) IsAllActive

func (h HoursJSON) IsAllActive() bool

IsAllActive then return the true

func (HoursJSON) IsNoActive

func (h HoursJSON) IsNoActive() bool

IsNoActive then return the true

func (HoursJSON) MarshalJSON

func (h HoursJSON) MarshalJSON() ([]byte, error)

MarshalJSON implements the functionality of json.Marshaler interface

func (HoursJSON) Merge

func (h HoursJSON) Merge(h2 Hours)

Merge from another hours

func (*HoursJSON) Scan

func (h *HoursJSON) Scan(value interface{}) (err error)

Scan - Implement the database/sql scanner interface

func (*HoursJSON) SetHour

func (h *HoursJSON) SetHour(weekDay time.Weekday, hour byte, active bool)

SetHour as active or no

func (HoursJSON) String

func (h HoursJSON) String() string

String implementation of fmt.Stringer

func (HoursJSON) TestHour

func (h HoursJSON) TestHour(weekDay time.Weekday, hour byte) bool

TestHour hour

func (HoursJSON) TestTime

func (h HoursJSON) TestTime(t time.Time) bool

TestTime hour

func (*HoursJSON) UnmarshalJSON

func (h *HoursJSON) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the functionality of json.Unmarshaler interface

func (HoursJSON) Value

func (h HoursJSON) Value() (driver.Value, error)

Value implementation of valuer for database/sql

Jump to

Keyboard shortcuts

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