bitset

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 2 Imported by: 1

README

bitset GoDoc Build Status Coverage Status Go Report Card

A simple bit set with JSON support

Motivation

The package built specially for usage in JWT tokens in package github.com/axkit/aaa

Concepts

  • Application functionality can be limited by using permissions.
  • Permission (access right) represented by unique string code.
  • Application can have many permissions.
  • A user has a role.
  • A role is set of allowed permission, it's subset of all permissions supported by application.
  • As a result of succesfull sign in backend provides access and resresh tokens.
  • Payload of access token shall have list of allowed permissions.
  • A single permission code looks like "Customers.Create", "Customer.AttachDocuments", "Customer.Edit", etc.
  • Store allowed permission codes could increase token size.
  • Bitset comes here.
  • Every permission shall be accociated with a single bit in the set.
  • Bitset adds to the token as hexadecimal string.

Usage Examples

Sign In

    var perms bitset.Bitset
    perms.Set(1)                    // 0000_0010
    perms.Set(2)                    // 0000_0110
    perms.Set(8, 10)                // 0000_0110 0000_0101
    tokenPerms := perms.String()    // returns "0605"

Check allowed permission in auth middleware

    ...
    tokenPerms := accessToken.Payload.Perms     // "0605
    bs := bitset.Parse(tokenPerms)              // returns 0000_0110 0000_0101
    if bs.AreSet(2,8) {
        // the permission allowed
    }

Further Improvements

  • Finalize integration BitSet with database/sql
  • Add benchmarks
  • Reduce memory allocations

Prague 2020

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreSet

func AreSet(buf []byte, bitpos ...uint) bool

AreSet recieves string representation of BitSet and returns true if every bit with position bitpos is equal 1.

Types

type BitSet

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

BitSet holds set of bits using slice of bytes.

func New

func New(size uint) BitSet

New creates BitSet with allocated space for size amount of bits.

func Parse

func Parse(buf []byte) BitSet

Parse converts []byte to BitSet.

func (*BitSet) AreSet

func (bs *BitSet) AreSet(bitpos ...uint) bool

AreSet returns true if every bit with position bit pos is equal 1. Return false if bitops or bs are empty.

func (*BitSet) IsAllocated

func (bs *BitSet) IsAllocated(bitpos uint) bool

IsAllocated returns true if space for the bit with position bitpos is allocated already.

func (*BitSet) IsSet

func (bs *BitSet) IsSet(bitpos uint) bool

IsSet returns true if bit with position bitpos is 1. Returns false if bitpos above maximal setted bitpos.

func (*BitSet) Len

func (bs *BitSet) Len() uint

Len returns len of allocated byte slice.

func (*BitSet) Reset

func (bs *BitSet) Reset(bitpos uint)

Reset sets bit with position bitpos to 0. Does not resize internal storage.

func (*BitSet) Scan

func (bs *BitSet) Scan(value interface{}) error

Scan implements database/sql Scanner. It's expected that PostgresSQL type BIT VARYING is used.

func (*BitSet) Set

func (bs *BitSet) Set(bitpos ...uint)

Set sets bits to 1. Extends internal storage if it's required.

func (*BitSet) String

func (bs *BitSet) String() string

String returns hex representation of bit array. Every 8 bits as 2 hex digits.

func (*BitSet) Valid

func (bs *BitSet) Valid() bool

Valid returns true if at least one bit is set.

func (BitSet) Value

func (bs BitSet) Value() (driver.Value, error)

Value implements database/sql Valuer.

Jump to

Keyboard shortcuts

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