Skip to content

Commit

Permalink
feat(hash): add hash module
Browse files Browse the repository at this point in the history
Merge pull request #51 from smintz/hash
  • Loading branch information
b5 authored May 14, 2020
2 parents b159b9d + 686ae7b commit f9d9f93
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 24 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Qri](https://img.shields.io/badge/made%20by-qri-magenta.svg?style=flat-square)](https://qri.io) [![GoDoc](https://godoc.org/github.com/qri-io/starlib?status.svg)](http://godoc.org/github.com/qri-io/starlib) [![License](https://img.shields.io/github/license/qri-io/starlib.svg?style=flat-square)](./LICENSE) [![Codecov](https://img.shields.io/codecov/c/github/qri-io/starlib.svg?style=flat-square)](https://codecov.io/gh/qri-io/starlib) [![CI](https://img.shields.io/circleci/project/github/qri-io/starlib.svg?style=flat-square)](https://circleci.com/gh/qri-io/starlib)


<div align="center">
<h3>
<a href="https://qri.io">
Expand Down Expand Up @@ -35,32 +34,33 @@

This is a community-driven project to bring a standard library to the starlark programming dialect. We here at Qri need a standard library, and we thought it might benefit others to structure this library in a reusable way. We are a little biased towards our needs, and will be shaping the library primarily toward's Qri's use case.

| Question | Answer |
|--------|-------|
| "What's starlark?" | It's a python-like scripting language open-sourced by Google. [Here are the docs](https://docs.bazel.build/versions/master/skylark/language.html) |
| "What's the use-case for this?" | [We're building it for Qri ('query')](https://qri.io) |
| "I want to play with starlib outside of Qri" | [Checkout the starlark playground](https://github.com/qri-io/skypg) |
| "I have a question" | [Create an issue](https://github.com/qri-io/starlib/issues) |
| "I found a bug" | [Create an issue](https://github.com/qri-io/starlib/issues) |
| "I would like to propose a new package" | You should think about [creating an RFC](https://github.com/qri-io/rfcs) |
| Question | Answer |
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| "What's starlark?" | It's a python-like scripting language open-sourced by Google. [Here are the docs](https://docs.bazel.build/versions/master/skylark/language.html) |
| "What's the use-case for this?" | [We're building it for Qri ('query')](https://qri.io) |
| "I want to play with starlib outside of Qri" | [Checkout the starlark playground](https://github.com/qri-io/skypg) |
| "I have a question" | [Create an issue](https://github.com/qri-io/starlib/issues) |
| "I found a bug" | [Create an issue](https://github.com/qri-io/starlib/issues) |
| "I would like to propose a new package" | You should think about [creating an RFC](https://github.com/qri-io/rfcs) |

## Packages

The following is a list of the packages currently in the standard library

| Package | Go Docs | Description |
|---------|---------|-------------|
| [`bsoup`](https://github.com/qri-io/starlib/tree/master/bsoup) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/bsoup?status.svg)](https://godoc.org/github.com/qri-io/starlib/bsoup) | a beautiful-soup-like API for working with HTML |
| [`encoding/base64`](https://github.com/qri-io/starlib/tree/master/encoding/base64) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/base64?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/base64) | base64 de/serialization |
| [`encoding/csv`](https://github.com/qri-io/starlib/tree/master/encoding/csv) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/csv?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/csv) | csv de/serialization |
| [`encoding/json`](https://github.com/qri-io/starlib/tree/master/encoding/json) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/json?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/json) | json de/serialization |
| [`geo`](https://github.com/qri-io/starlib/tree/master/geo) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/geo?status.svg)](https://godoc.org/github.com/qri-io/starlib/geo) | 2d geographic operations |
| [`html`](https://github.com/qri-io/starlib/tree/master/html) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/html?status.svg)](https://godoc.org/github.com/qri-io/starlib/html) | html text processing |
| [`http`](https://github.com/qri-io/starlib/tree/master/http) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/http?status.svg)](https://godoc.org/github.com/qri-io/starlib/http) | http client operations |
| [`math`](https://github.com/qri-io/starlib/tree/master/math) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/math?status.svg)](https://godoc.org/github.com/qri-io/starlib/math) | mathematical functions & values |
| [`re`](https://github.com/qri-io/starlib/tree/master/re) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/re?status.svg)](https://godoc.org/github.com/qri-io/starlib/re) | regular expressions |
| [`time`](https://github.com/qri-io/starlib/tree/master/time) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/time?status.svg)](https://godoc.org/github.com/qri-io/starlib/time) | time operations |
| [`xlsx`](https://github.com/qri-io/starlib/tree/master/xlsx) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/xlsx?status.svg)](https://godoc.org/github.com/qri-io/starlib/xlsx) | xlsx file format reading |
| [`zipfile`](https://github.com/qri-io/starlib/tree/master/zipfile) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/zipfile?status.svg)](https://godoc.org/github.com/qri-io/starlib/zipfile) | support for zip archives |
| Package | Go Docs | Description |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------- |
| [`bsoup`](https://github.com/qri-io/starlib/tree/master/bsoup) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/bsoup?status.svg)](https://godoc.org/github.com/qri-io/starlib/bsoup) | a beautiful-soup-like API for working with HTML |
| [`encoding/base64`](https://github.com/qri-io/starlib/tree/master/encoding/base64) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/base64?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/base64) | base64 de/serialization |
| [`encoding/csv`](https://github.com/qri-io/starlib/tree/master/encoding/csv) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/csv?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/csv) | csv de/serialization |
| [`encoding/json`](https://github.com/qri-io/starlib/tree/master/encoding/json) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/encoding/json?status.svg)](https://godoc.org/github.com/qri-io/starlib/encoding/json) | json de/serialization |
| [`geo`](https://github.com/qri-io/starlib/tree/master/geo) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/geo?status.svg)](https://godoc.org/github.com/qri-io/starlib/geo) | 2d geographic operations |
| [`hash`](https://github.com/qri-io/starlib/tree/master/hash) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/hash?status.svg)](https://godoc.org/github.com/qri-io/starlib/hash) | hash operations (md5/sha1/sha256) |
| [`html`](https://github.com/qri-io/starlib/tree/master/html) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/html?status.svg)](https://godoc.org/github.com/qri-io/starlib/html) | html text processing |
| [`http`](https://github.com/qri-io/starlib/tree/master/http) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/http?status.svg)](https://godoc.org/github.com/qri-io/starlib/http) | http client operations |
| [`math`](https://github.com/qri-io/starlib/tree/master/math) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/math?status.svg)](https://godoc.org/github.com/qri-io/starlib/math) | mathematical functions & values |
| [`re`](https://github.com/qri-io/starlib/tree/master/re) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/re?status.svg)](https://godoc.org/github.com/qri-io/starlib/re) | regular expressions |
| [`time`](https://github.com/qri-io/starlib/tree/master/time) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/time?status.svg)](https://godoc.org/github.com/qri-io/starlib/time) | time operations |
| [`xlsx`](https://github.com/qri-io/starlib/tree/master/xlsx) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/xlsx?status.svg)](https://godoc.org/github.com/qri-io/starlib/xlsx) | xlsx file format reading |
| [`zipfile`](https://github.com/qri-io/starlib/tree/master/zipfile) | <img width=190/>[![Go Docs](https://godoc.org/github.com/qri-io/starlib/zipfile?status.svg)](https://godoc.org/github.com/qri-io/starlib/zipfile) | support for zip archives |

###### This documentation has been adapted from the [Cycle.js](https://github.com/cyclejs/cyclejs) documentation.
###### This documentation has been adapted from the [Cycle.js](https://github.com/cyclejs/cyclejs) documentation.
17 changes: 17 additions & 0 deletions hash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# time

hash defines hash primitives for starlark

## Functions

#### `md5(string) string`

returns md5 hash of a string

#### `sha1(string) string`

returns sha1 hash of a string

#### `sha256(string) string`

returns sha256 hash of a string
14 changes: 14 additions & 0 deletions hash/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*Package hash defines hash primitives for starlark.
outline:
hash defines hash primitives for starlark.
path: hash
functions:
md5(string) string
returns an md5 hash for a string
sha1(string) string
returns an sha1 hash for a string
sha256(string) string
returns an sha256 hash for a string
*/
package hash
56 changes: 56 additions & 0 deletions hash/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package hash

import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"sync"

"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)

// ModuleName defines the expected name for this Module when used
// in starlark's load() function, eg: load('hash.star', 'hash')
const ModuleName = "hash.star"

var (
once sync.Once
hashModule starlark.StringDict
hashError error
)

// LoadModule loads the time module.
// It is concurrency-safe and idempotent
func LoadModule() (starlark.StringDict, error) {
once.Do(func() {
hashModule = starlark.StringDict{
"hash": &starlarkstruct.Module{
Name: "hash",
Members: starlark.StringDict{
"md5": starlark.NewBuiltin("hash.md5", fnHash(md5.New)),
"sha1": starlark.NewBuiltin("hash.sha1", fnHash(sha1.New)),
"sha256": starlark.NewBuiltin("hash.sha256", fnHash(sha256.New)),
},
},
}

})
return hashModule, hashError

}

func fnHash(hash func() hash.Hash) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
return func(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var s starlark.String
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &s); err != nil {
return nil, err
}

h := hash()
h.Write([]byte(string(s)))
return starlark.String(fmt.Sprintf("%x", h.Sum(nil))), nil
}
}
22 changes: 22 additions & 0 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hash

import (
"testing"

"github.com/qri-io/starlib/testdata"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/starlarktest"
)

func TestFile(t *testing.T) {
resolve.AllowFloat = true
thread := &starlark.Thread{Load: testdata.NewLoader(LoadModule, ModuleName)}
starlarktest.SetReporter(thread, t)

// Execute test file
_, err := starlark.ExecFile(thread, "testdata/test.star", nil, nil)
if err != nil {
t.Error(err)
}
}
9 changes: 9 additions & 0 deletions hash/testdata/test.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Test data for hash module
"""
load('hash.star', 'hash')
load('assert.star', 'assert')

assert.eq(hash.md5("helloworld"), "fc5e038d38a57032085441e7fe7010b0")
assert.eq(hash.sha1("helloworld"), "6adfb183a4a2c94a2f92dab5ade762a47889a5a1")
assert.eq(hash.sha256("helloworld"), "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af")
3 changes: 3 additions & 0 deletions starlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/qri-io/starlib/encoding/json"
"github.com/qri-io/starlib/encoding/yaml"
"github.com/qri-io/starlib/geo"
"github.com/qri-io/starlib/hash"
"github.com/qri-io/starlib/html"
"github.com/qri-io/starlib/http"
"github.com/qri-io/starlib/math"
Expand Down Expand Up @@ -51,6 +52,8 @@ func Loader(thread *starlark.Thread, module string) (dict starlark.StringDict, e
return geo.LoadModule()
case math.ModuleName:
return math.LoadModule()
case hash.ModuleName:
return hash.LoadModule()
}

return nil, fmt.Errorf("invalid module '%s'", module)
Expand Down

0 comments on commit f9d9f93

Please sign in to comment.