Commit 1c74d493 authored by Hector Sanjuan's avatar Hector Sanjuan

Extract from go-ipfs: readme, travis, golint, makefile, tests...

parent 1880bdda
sudo: false
language: go
go:
- 1.9
install:
- go get github.com/whyrusleeping/gx
- go get github.com/whyrusleeping/gx-go
script:
- make test
after_success:
- bash <(curl -s https://codecov.io/bash)
cache:
directories:
- $GOPATH/src/gx
notifications:
email: false
MIT License
Copyright (c) 2018 IPFS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
all: deps
gx:
go get github.com/whyrusleeping/gx
go get github.com/whyrusleeping/gx-go
deps: gx
gx --verbose install --global
gx-go rewrite
test: deps
gx test -v -race -coverprofile=coverage.txt -covermode=atomic .
rw:
gx-go rewrite
rwundo:
gx-go rewrite --undo
publish: rwundo
gx publish
.PHONY: all gx deps test rw rwundo publish
# go-ipfs-ds-help
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![GoDoc](https://godoc.org/github.com/ipfs/go-ipfs-ds-help?status.svg)](https://godoc.org/github.com/ipfs/go-ipfs-ds-help)
[![Build Status](https://travis-ci.org/ipfs/go-ipfs-ds-help.svg?branch=master)](https://travis-ci.org/ipfs/go-ipfs-ds-help)
> go-ipfs-ds-help provides utilities for parsing and creating datastore keys used by go-ipfs.
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)
## Install
`go-ipfs-ds-help` works like a regular Go module:
```
> go get github.com/ipfs/go-ipfs-ds-help
```
## Usage
```
import "github.com/ipfs/go-ipfs-ds-help"
```
Check the [GoDoc documentation](https://godoc.org/github.com/ipfs/go-ipfs-ds-help)
This module uses [Gx](https://github.com/whyrusleeping/gx) to manage dependencies. You can use `make all` to build it with the `gx` dependencies.
## Contribute
PRs accepted.
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
## License
MIT © Protocol Labs, Inc.
// Package dshelp provides utilities for parsing and creating
// datastore keys used by go-ipfs
package dshelp
import (
ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
base32 "gx/ipfs/QmfVj3x4D6Jkq9SEoi5n2NmoUomLwoeiwnYz2KQa15wRw6/base32"
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/whyrusleeping/base32"
)
// TODO: put this code into the go-datastore itself
func NewKeyFromBinary(rawKey []byte) ds.Key {
// NewKeyFromBinary creates a new key from a byte slice.
func NewKeyFromBinary(rawKey []byte) datastore.Key {
buf := make([]byte, 1+base32.RawStdEncoding.EncodedLen(len(rawKey)))
buf[0] = '/'
base32.RawStdEncoding.Encode(buf[1:], rawKey)
return ds.RawKey(string(buf))
return datastore.RawKey(string(buf))
}
func BinaryFromDsKey(k ds.Key) ([]byte, error) {
// BinaryFromDsKey returns the byte slice corresponding to the given Key.
func BinaryFromDsKey(k datastore.Key) ([]byte, error) {
return base32.RawStdEncoding.DecodeString(k.String()[1:])
}
func CidToDsKey(k *cid.Cid) ds.Key {
// CidToDsKey creates a Key from the given Cid.
func CidToDsKey(k *cid.Cid) datastore.Key {
return NewKeyFromBinary(k.Bytes())
}
func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
// DsKeyToCid converts the given Key to its corresponding Cid.
func DsKeyToCid(dsKey datastore.Key) (*cid.Cid, error) {
kb, err := BinaryFromDsKey(dsKey)
if err != nil {
return nil, err
......
package dshelp
import (
"testing"
cid "github.com/ipfs/go-cid"
)
func TestKey(t *testing.T) {
c, _ := cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq")
dsKey := CidToDsKey(c)
c2, err := DsKeyToCid(dsKey)
if err != nil {
t.Fatal(err)
}
if c.String() != c2.String() {
t.Fatal("should have parsed the same key")
}
}
{
"author": "hector",
"bugs": {
"url": "https://github.com/ipfs/go-ipfs-ds-help"
},
"gx": {
"dvcsimport": "github.com/ipfs/go-ipfs-ds-help"
},
"gxDependencies": [
{
"author": "jbenet",
"hash": "QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg",
"name": "go-datastore",
"version": "1.4.1"
},
{
"author": "whyrusleeping",
"hash": "QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY",
"name": "go-cid",
"version": "0.7.20"
},
{
"author": "whyrusleeping",
"hash": "QmfVj3x4D6Jkq9SEoi5n2NmoUomLwoeiwnYz2KQa15wRw6",
"name": "base32",
"version": "0.0.2"
}
],
"gxVersion": "0.12.1",
"language": "go",
"license": "",
"name": "go-ipfs-ds-help",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.0.0"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment