Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update uuid library #124

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cz.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.commitizen]
name = "cz_conventional_commits"
tag_format = "$version"
version_scheme = "semver"
version = "0.0.1"
56 changes: 56 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Go 1.22
uses: actions/setup-go@v3
with:
go-version: 1.22

- name: Cache Go modules
uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Update dependencies
run: go mod tidy

- name: Test with Coverage
run: go test ./... -coverprofile=coverage.out -covermode=atomic

- name: Check Coverage
run: |
go tool cover -func=coverage.out -o coverage-summary.txt
COVERAGE=$(go tool cover -func=coverage.out | grep total: | awk '{print substr($3, 1, length($3)-1)}')
echo "Total test coverage: $COVERAGE%"
if (( $(echo "$COVERAGE < 60" |bc -l) )); then
echo "Test coverage is below 60%"
exit 1
fi
env:
GO111MODULE: on

- name: Install golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest

- name: Run golangci-lint
run: golangci-lint run ./...
env:
GO111MODULE: on
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea/
# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.PHONY: install-tools check test

# Go tool paths
GOLINT = $(shell go env GOPATH)/bin/golint
INEFFASSIGN = $(shell go env GOPATH)/bin/ineffassign
MISSPELL = $(shell go env GOPATH)/bin/misspell
GOCYCLO = $(shell go env GOPATH)/bin/gocyclo

install-tools:
@echo "Installing tools..."
go install golang.org/x/lint/golint@latest
go install github.com/gordonklaus/ineffassign@latest
go install github.com/client9/misspell/cmd/misspell@latest
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest

check: install-tools
@echo "Running checks..."
go fmt ./...
go vet ./...
$(GOLINT) ./...
$(MISSPELL) -w .
$(GOCYCLO) -over 10 .
$(INEFFASSIGN) .

test:
@echo "Running tests..."
go test -coverprofile=coverage.out -coverpkg=$$(go list ./... | grep -v /test$$ | grep -v main | grep -v '_repository.go$$' | tr '\n' ',') ./...

coverage-report: test
@echo "Generating coverage report..."
go tool cover -func=coverage.out | grep total | awk '{print substr($$NF, 1, length($$NF)-1)}' > coverage.txt
@COVERAGE=$$(cat coverage.txt); \
echo "Coverage: $$COVERAGE"; \
if (( $$(echo "$$COVERAGE < 30" | bc -l) )); then \
echo "Coverage is below 30%. Stopping the process."; \
exit 1; \
fi
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# UUID package for Go language

[![Build Status](https://travis-ci.org/satori/go.uuid.svg?branch=master)](https://travis-ci.org/satori/go.uuid)
![CI](https://github.com/satori/go.uuid/actions/workflows/actions.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/satori/go.uuid)](https://goreportcard.com/report/github.com/satori/go.uuid)
[![Coverage Status](https://coveralls.io/repos/github/satori/go.uuid/badge.svg?branch=master)](https://coveralls.io/github/satori/go.uuid)
[![GoDoc](http://godoc.org/github.com/satori/go.uuid?status.svg)](http://godoc.org/github.com/satori/go.uuid)

Expand Down
104 changes: 48 additions & 56 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
// FromBytes returns UUID converted from raw byte slice input.
// It will return error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (u UUID, err error) {
if len(input) != Size {
return Nil, fmt.Errorf("uuid: expected %d bytes, got %d bytes", Size, len(input))
}
err = u.UnmarshalBinary(input)
return
}
Expand All @@ -48,6 +51,9 @@ func FromBytesOrNil(input []byte) UUID {
// Input is expected in a form accepted by UnmarshalText.
func FromString(input string) (u UUID, err error) {
err = u.UnmarshalText([]byte(input))
if err != nil {
return Nil, fmt.Errorf("uuid: failed to parse UUID from string: %s", input)
}
return
}

Expand All @@ -70,27 +76,30 @@ func (u UUID) MarshalText() (text []byte, err error) {

// UnmarshalText implements the encoding.TextUnmarshaler interface.
// Following formats are supported:
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
// "6ba7b8109dad11d180b400c04fd430c8"
//
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
// "6ba7b8109dad11d180b400c04fd430c8"
//
// ABNF for supported UUID text representation follows:
// uuid := canonical | hashlike | braced | urn
// plain := canonical | hashlike
// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
// hashlike := 12hexoct
// braced := '{' plain '}'
// urn := URN ':' UUID-NID ':' plain
// URN := 'urn'
// UUID-NID := 'uuid'
// 12hexoct := 6hexoct 6hexoct
// 6hexoct := 4hexoct 2hexoct
// 4hexoct := 2hexoct 2hexoct
// 2hexoct := hexoct hexoct
// hexoct := hexdig hexdig
// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
// 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
//
// uuid := canonical | hashlike | braced | urn
// plain := canonical | hashlike
// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
// hashlike := 12hexoct
// braced := '{' plain '}'
// urn := URN ':' UUID-NID ':' plain
// URN := 'urn'
// UUID-NID := 'uuid'
// 12hexoct := 6hexoct 6hexoct
// 6hexoct := 4hexoct 2hexoct
// 4hexoct := 2hexoct 2hexoct
// 2hexoct := hexoct hexoct
// hexoct := hexdig hexdig
// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
// 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
func (u *UUID) UnmarshalText(text []byte) (err error) {
switch len(text) {
case 32:
Expand All @@ -112,65 +121,51 @@ func (u *UUID) UnmarshalText(text []byte) (err error) {
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
func (u *UUID) decodeCanonical(t []byte) (err error) {
if t[8] != '-' || t[13] != '-' || t[18] != '-' || t[23] != '-' {
return fmt.Errorf("uuid: incorrect UUID format %s", t)
return fmt.Errorf("uuid: incorrect UUID format %s", string(t))
}

src := t[:]
dst := u[:]

for i, byteGroup := range byteGroups {
if i > 0 {
src = src[1:] // skip dash
t = t[1:] // skip dash
}
_, err = hex.Decode(dst[:byteGroup/2], src[:byteGroup])
if err != nil {
return
if _, err := hex.Decode(dst[:byteGroup/2], t[:byteGroup]); err != nil {
return err
}
src = src[byteGroup:]
t = t[byteGroup:]
dst = dst[byteGroup/2:]
}

return
return nil
}

// decodeHashLike decodes UUID string in format
// "6ba7b8109dad11d180b400c04fd430c8".
func (u *UUID) decodeHashLike(t []byte) (err error) {
src := t[:]
dst := u[:]

if _, err = hex.Decode(dst, src); err != nil {
return err
if _, err = hex.Decode(u[:], t); err != nil {
return fmt.Errorf("uuid: failed to decode hash-like format: %s", string(t))
}
return
return nil
}

// decodeBraced decodes UUID string in format
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" or in format
// "{6ba7b8109dad11d180b400c04fd430c8}".
func (u *UUID) decodeBraced(t []byte) (err error) {
l := len(t)

if t[0] != '{' || t[l-1] != '}' {
return fmt.Errorf("uuid: incorrect UUID format %s", t)
if len(t) < 2 || t[0] != '{' || t[len(t)-1] != '}' {
return fmt.Errorf("uuid: incorrect UUID format %s", string(t))
}

return u.decodePlain(t[1 : l-1])
return u.decodePlain(t[1 : len(t)-1])
}

// decodeURN decodes UUID string in format
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in format
// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8".
func (u *UUID) decodeURN(t []byte) (err error) {
total := len(t)

urn_uuid_prefix := t[:9]

if !bytes.Equal(urn_uuid_prefix, urnPrefix) {
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
if len(t) < 9 || !bytes.Equal(t[:9], urnPrefix) {
return fmt.Errorf("uuid: incorrect URN format: %s", string(t))
}

return u.decodePlain(t[9:total])
return u.decodePlain(t[9:])
}

// decodePlain decodes UUID string in canonical format
Expand All @@ -183,24 +178,21 @@ func (u *UUID) decodePlain(t []byte) (err error) {
case 36:
return u.decodeCanonical(t)
default:
return fmt.Errorf("uuid: incorrrect UUID length: %s", t)
return fmt.Errorf("uuid: incorrect UUID length: %s", string(t))
}
}

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (u UUID) MarshalBinary() (data []byte, err error) {
data = u.Bytes()
return
return u.Bytes(), nil
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It will return error if the slice isn't 16 bytes long.
func (u *UUID) UnmarshalBinary(data []byte) (err error) {
if len(data) != Size {
err = fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
return
return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
}
copy(u[:], data)

return
return nil
}
Loading