nekomeowww / bitmask

A golang library to manipulating bitmasks with marshal/unmarshal struct support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bitmask

build unittest

A golang library to manipulating bitmasks with marshal/unmarshal struct support.

Features:

  • Manually manipulating bitmasks with easy to use functions
  • Marshal/Unmarshal bitmasks into or from structs
  • Support all int, uint types, and bool in structs

Usage

Important notice: The first bit should be 1, not 0.

Use Set(int) and Unset(int) to set and unset bits. Use IsSet(int) bool to determine if a bit is set.

Use Marshal(interface{}) to marshal a bitmask into a struct. Use Unmarshal(interface{}) to unmarshal a bitmask from a struct.

Manually

package main

import "github.com/nekomeowww/bitmask"

func main() {
    b := bitmask.New(3)
    b.Set(1)
    b.Set(2)

    b.IsSet(1) // true
    b.IsSet(2) // true
    b.IsSet(3) // false

    b.Unset(1)
    b.IsSet(1) // false
}

Marshal

Use the tag bitmask to specify the bitmask field.

Int

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
   A int `bitmask:"1"`
   B int `bitmask:"2"`
}

func main() {
    v := tS1{A: 1, B: 1}
    b, err := Marshal(v)
    if err != nil {
        log.Fatal(err)
    }

    b.IsSet(1) // true
    b.IsSet(2) // true
}

Uint

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
   A uint `bitmask:"1"`
   B uint `bitmask:"2"`
}

func main() {
    v := tS1{A: 1, B: 1}
    b, err := Marshal(v)
    if err != nil {
        log.Fatal(err)
    }

    b.IsSet(1) // true
    b.IsSet(2) // true
}

Bool

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
   A bool `bitmask:"1"`
   B bool `bitmask:"2"`
}

func main() {
    v := tS1{A: true, B: true}
    b, err := Marshal(v)
    if err != nil {
        log.Fatal(err)
    }

    b.IsSet(1) // true
    b.IsSet(2) // true
}

Unmarshal

Use the tag bitmask to specify the bitmask field.

Int

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
    A int `bitmask:"1"`
    B int `bitmask:"2"`
}

func main() {
    var v tS1

    b := New(3)
    b.IsSet(1) // true
    b.IsSet(2) // true

    err := Unmarshal(bitmask, &v)
    if err != nil {
        log.Fatal(err)
    }

    v.A // 1
    v.B // 1
}

Uint

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
    A uint `bitmask:"1"`
    B uint `bitmask:"2"`
}

func main() {
    var v tS1

    b := New(3)
    b.IsSet(1) // true
    b.IsSet(2) // true

    err := Unmarshal(bitmask, &v)
    if err != nil {
        log.Fatal(err)
    }

    v.A // 1
    v.B // 1
}

Bool

package main

import "github.com/nekomeowww/bitmask"

type tS1 struct {
    A bool `bitmask:"1"`
    B bool `bitmask:"2"`
}

func main() {
    var v tS1

    b := New(3)
    b.IsSet(1) // true
    b.IsSet(2) // true

    err := Unmarshal(bitmask, &v)
    if err != nil {
        log.Fatal(err)
    }

    v.A // true
    v.B // true
}

Error handling

List of errors:

var (
    // ErrInvalidTagValue tag 值不合法 (tag value is invalid)
    ErrInvalidTagValue = errors.New("invalid tag value")
    // ErrNoNilPointerAllowed 不允许 nil 指针 (nil pointer is not allowed) 
    ErrNoNilPointerAllowed = errors.New("no nil pointer allowed")
    // ErrNotAStructPointer 不是结构体指针 (not a struct pointer)
    ErrNotAStructPointer = errors.New("dest must be a pointer to struct")
    // ErrNotStructPointerOrStruct 不是结构体指针或结构体 (not a struct pointer or struct)
    ErrNotStructPointerOrStruct = errors.New("dest must be a pointer to struct or a struct")
    // ErrBitOutOfRange bit 超出范围 (bit out of range)
    ErrBitOutOfRange = errors.New("bit should within 1 to 64")
)

About

A golang library to manipulating bitmasks with marshal/unmarshal struct support.

License:MIT License


Languages

Language:Go 100.0%