{"id":15914458,"url":"https://github.com/bbengfort/sequence","last_synced_at":"2025-04-03T03:27:37.889Z","repository":{"id":24532364,"uuid":"27938676","full_name":"bbengfort/sequence","owner":"bbengfort","description":"Implements an AutoIncrement counter class similar to PostgreSQL's sequence.","archived":false,"fork":false,"pushed_at":"2017-10-24T12:20:03.000Z","size":24,"stargazers_count":1,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T17:30:52.319Z","etag":null,"topics":["go","monotonicity","sequence"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bbengfort.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-12T21:22:47.000Z","updated_at":"2023-09-08T16:52:55.000Z","dependencies_parsed_at":"2022-08-23T00:10:47.938Z","dependency_job_id":null,"html_url":"https://github.com/bbengfort/sequence","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbengfort%2Fsequence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbengfort%2Fsequence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbengfort%2Fsequence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbengfort%2Fsequence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbengfort","download_url":"https://codeload.github.com/bbengfort/sequence/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246930815,"owners_count":20856695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","monotonicity","sequence"],"created_at":"2024-10-06T17:03:05.625Z","updated_at":"2025-04-03T03:27:37.871Z","avatar_url":"https://github.com/bbengfort.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go-Sequence\n\n[![Build Status](https://travis-ci.org/bbengfort/sequence.svg?branch=master)](https://travis-ci.org/bbengfort/sequence)\n[![Coverage Status](https://coveralls.io/repos/github/bbengfort/sequence/badge.svg?branch=master)](https://coveralls.io/github/bbengfort/sequence?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/bbengfort/sequence)](https://goreportcard.com/report/github.com/bbengfort/sequence)\n[![GoDoc](https://godoc.org/github.com/bbengfort/sequence?status.svg)](https://godoc.org/github.com/bbengfort/sequence)\n\n**Implements an AutoIncrement counter class similar to PostgreSQL's sequence.**\n\nThis library provides monotonically increasing and decreasing sequences similar to the autoincrement sequence objects that are available in databases like PostgreSQL. Unlike simple counter objects, `Sequence` objects are bred for safety, that is they expose the largest possible range of positive integers using the `uint64` data type and raise exceptions when that number overflows or when the increment function does something unexpected. Moreover, the internal state of a `Sequence` is not accessible by external libraries and therefore cannot be modified (except for a straight-up reset), giving developers confidence to use these objects in sequence-critical usage such as automatically incrementing IDs.\n\n## Getting Started\n\nTo install the sequence library, simply `go get` it from GitHub:\n\n```\n$ go get github.com/bbengfort/sequence\n```\n\nFor more specifics, please read the [API documentation](https://godoc.org/github.com/bbengfort/sequence).\n\n### Basic Usage\n\nThe basic usage is to create a default, monotonically incrementing by 1 sequence that starts at 1 and goes until 18,446,744,073,709,551,614 (the largest possible `uint64` value).\n\n```go\nimport github.com/bbengfort/sequence\n\nseq := sequence.New()\nidx, err := seq.Next()\n```\n\nA range can be specified using different arguments to New. For example, to specify a different maximal value:\n\n```go\nseq := sequence.New(1000)\n```\n\nWill produce a sequence from 1 to 1000 (inclusive) and will return errors when the state goes beyond 1000. A specifically bounded range:\n\n```go\nseq := sequence.New(10, 100)\n```\n\nWill provide a sequence on the integers from 10 to 100 (inclusive). Finally a step can be provided to determine how the sequence is incremented:\n\n```go\nseq := sequence.New(2, 500, 2)\n```\n\nWhich will return all the even numbers from 2 to 500 (inclusive). Sequences can be reset, returning them to their original state as follows:\n\n```go\nerr := seq.Reset()\n```\n\nSequences can also be updated to a specific value, so long as the value doesn't violate the monotonically increasing or decreasing rule. If it does, the `Update()` function will return an error:\n\n```go\nerr := seq.Update(42) // err == nil\nseq.Next()\nerr = seq.Update(42) // err != nil\n```\n\nNote that the `Reset()` method is the only function that could violate the monotonicity of the `Sequence` object.  \n\n### Sequence State\n\nTo get the state of a sequence, you can use the following methods:\n\n```go\nseq.IsStarted() // Returns a boolean value if the Sequence is started.\n\n// Get the current value of the sequence\nidx, err := seq.Current()\n\n// Print a string representation of the sequence state.\nfmt.Println(seq.String())\n```\n\nYou can also serialize and deserialize the Sequence to pass it across processes as follows:\n\n```go\ndata, err := seq.Dump()\nseq2 := \u0026sequence.Sequence{}\nerr := seq2.Load(data)\n```\n\nThis snippet of code will result in `seq2` having an identical state to `seq` at the moment that it was dumped.\n\n## Development\n\nPull requests are more than welcome to help develop this project!\n\n### Testing\n\nTo execute tests for this library:\n\n```\n$ make test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbengfort%2Fsequence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbengfort%2Fsequence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbengfort%2Fsequence/lists"}