diff --git a/.travis.yml b/.travis.yml
index bb7a788..9879b53 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,17 +7,16 @@ branches:
# skip tags build, we are building branch and master that is enough for
# consistenty check and release. Let's use Travis CI resources optimally
# for aah framework.
- - /^v[0-9]\.[0-9]/
+ - /^v[0-9.]+$/
go:
- - 1.8
- - 1.9
+ - 1.9.x
+ - 1.10.x
- tip
go_import_path: aahframework.org/essentials.v0
install:
- - git config --global http.https://aahframework.org.followRedirects true
- go get -t -v ./...
script:
diff --git a/LICENSE b/LICENSE
index 491ce72..02ad2ea 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2016-2017 Jeevanandam M., https://myjeeva.com
+
+ Essentials contains simple & useful util functions for Go. aah framework utilizes essentials (aka `ess`) library across. Essentials library complements to standard packages, refer Godoc to know more.Essentials by aah framework
+
+
+ -***v0.7 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Sep 29, 2017*** +### News -`essentials` contains simple & useful utils methods for Go. aah framework utilizes essentials (aka `ess`) library across. Essentials library complements with handy methods, refer godoc to know more about methods: + * `v0.8.0` [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Jun 20, 2018. - * filepath - * GUID (Globally Unique Identifier - Mongo Object ID algorithm) - * random string, random byte generation at fixed length - * go - * io - * os - * reflect - * string - * encode - * archive (zip) +## Installation -*`essentials` developed for aah framework. However, it's an independent library, can be used separately with any `Go` language project. Feel free to use it.* - -# Installation -#### Stable Version - Production Ready -```sh -# install the library +```bash go get -u aahframework.org/essentials.v0 ``` -Visit official website https://aahframework.org to learn more. +Visit official website https://aahframework.org to learn more about `aah` framework. diff --git a/essentials.go b/essentials.go new file mode 100644 index 0000000..6b7f10d --- /dev/null +++ b/essentials.go @@ -0,0 +1,11 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// aahframework.org/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +// Valuer interface is general purpose to `Set` and `Get` operations. +type Valuer interface { + Get(key string) interface{} + Set(key string, value interface{}) +} diff --git a/filepath.go b/filepath.go index 7871280..6031895 100644 --- a/filepath.go +++ b/filepath.go @@ -120,6 +120,10 @@ func Walk(srcDir string, walkFn filepath.WalkFunc) error { func doWalk(fname string, linkName string, walkFn filepath.WalkFunc) error { fsWalkFn := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + var name string name, err = filepath.Rel(fname, path) if err != nil { @@ -135,7 +139,6 @@ func doWalk(fname string, linkName string, walkFn filepath.WalkFunc) error { return err } - // https://github.com/golang/go/blob/master/src/path/filepath/path.go#L392 info, err = os.Lstat(symlinkPath) if err != nil { return walkFn(path, info, err) diff --git a/guid.go b/guid.go index e991d7b..036ebea 100644 --- a/guid.go +++ b/guid.go @@ -38,7 +38,7 @@ var ( // Package methods //___________________________________ -// NewGUID method returns a new Globally Unique identifier (GUID). +// NewGUID method returns a new Globally Unique Identifier (GUID). // // The 12-byte `UniqueId` consists of- // - 4-byte value representing the seconds since the Unix epoch, @@ -46,8 +46,8 @@ var ( // - 2-byte process id, and // - 3-byte counter, starting with a random value. // -// NewGUID generation using Mongo Object ID algorithm to generate globally -// unique ids - https://docs.mongodb.com/manual/reference/method/ObjectId/ +// Uses Mongo Object ID algorithm to generate globally unique ids - +// https://docs.mongodb.com/manual/reference/method/ObjectId/ func NewGUID() string { var b [12]byte // Timestamp, 4 bytes, big endian @@ -77,6 +77,8 @@ func readRandomUint32() uint32 { return (uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24) } + // To initialize package unexported variable 'guidCounter'. + // This panic would happen at program startup, so no worries at runtime panic. panic(errors.New("ess - guid: unable to generate random object id")) } @@ -97,6 +99,7 @@ func readMachineID() []byte { return id } - // return nil, errors.New("guid: unable to get hostname and random bytes") + // To initialize package unexported variable 'machineID'. + // This panic would happen at program startup, so no worries at runtime panic. panic(errors.New("ess - guid: unable to get hostname and random bytes")) } diff --git a/string.go b/string.go index f60896c..c779808 100644 --- a/string.go +++ b/string.go @@ -6,6 +6,9 @@ package ess import "strings" +// StringEmpty is empty string constant. Using `ess.StringEmpty` instead of "". +const StringEmpty = "" + // IsStrEmpty returns true if strings is empty otherwise false func IsStrEmpty(v string) bool { return len(strings.TrimSpace(v)) == 0 diff --git a/url.go b/url.go new file mode 100644 index 0000000..667283b --- /dev/null +++ b/url.go @@ -0,0 +1,28 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// aahframework.org/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +import "net/url" + +// IsVaildURL method returns true if given raw URL gets parsed without any errors +// otherwise false. +func IsVaildURL(rawurl string) bool { + _, err := url.Parse(rawurl) + return err == nil +} + +// IsRelativeURL method returns true if given raw URL is relative URL otherwise false. +func IsRelativeURL(rawurl string) bool { + return !IsAbsURL(rawurl) +} + +// IsAbsURL method returns true if given raw URL is absolute URL otherwise false. +func IsAbsURL(rawurl string) bool { + u, err := url.Parse(rawurl) + if err != nil { + return false + } + return u.IsAbs() +} diff --git a/url_test.go b/url_test.go new file mode 100644 index 0000000..db6849a --- /dev/null +++ b/url_test.go @@ -0,0 +1,54 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// aahframework.org/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +import ( + "testing" + + "aahframework.org/test.v0/assert" +) + +func TestURLValue(t *testing.T) { + t.Log("Valid URLs") + for _, u := range []string{ + "https://aahframework.org", + "/facebook-auth/callback?code=AQD5-i29_Vn7fNg8VgcV-Uzk_QNO1sx6yO-tJvkRiFaGs1n-wnCxUnvLX0V2q25Tx7JRZAiTds2-DIKrDb8jPEdGquMCedQ_mpMZQxsHmPYeg_cP1Xjy2jHooK-1ZDJZQHXtDSL8FA7r3nVA7WcrCuLZrlrgXq8LnOSAil3oMD-RqPix-nI576GvAPGgiXo6ep_AfS2GaF8A8TOTwl2iwEjB74F23yEukNpz5tmDJVfH02qtrGECuDfaAEPc-4u2wVIIWKCWy3oEEeoEr4zBdzsMtUR1FP3X5yUm0_yAYFP3taAPrpM-5UqtJWmUgaOY-U0&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac", + "https://www.facebook.com/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac", + "https://godoc.org/golang.org/x/oauth2", + "http://godoc.org/", + } { + assert.True(t, IsVaildURL(u)) + } + + t.Log("Absolute URL") + for _, u := range []string{ + "https://aahframework.org", + "https://www.facebook.com/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac", + "https://godoc.org/golang.org/x/oauth2", + "http://godoc.org/", + } { + assert.True(t, IsAbsURL(u)) + } + + t.Log("Error - Absolute URL") + for _, u := range []string{ + "://aahframework.org", + "://", + "http", + "https", + } { + assert.False(t, IsAbsURL(u)) + } + + t.Log("Relative URL") + for _, u := range []string{ + "/facebook-auth/callback?code=AQD5-i29_Vn7fNg8VgcV-Uzk_QNO1sx6yO-tJvkRiFaGs1n-wnCxUnvLX0V2q25Tx7JRZAiTds2-DIKrDb8jPEdGquMCedQ_mpMZQxsHmPYeg_cP1Xjy2jHooK-1ZDJZQHXtDSL8FA7r3nVA7WcrCuLZrlrgXq8LnOSAil3oMD-RqPix-nI576GvAPGgiXo6ep_AfS2GaF8A8TOTwl2iwEjB74F23yEukNpz5tmDJVfH02qtrGECuDfaAEPc-4u2wVIIWKCWy3oEEeoEr4zBdzsMtUR1FP3X5yUm0_yAYFP3taAPrpM-5UqtJWmUgaOY-U0&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac", + "/dialog/oauth?client_id=182958108394860&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Ffacebook-auth%2Fcallback&response_type=code&scope=public_profile+email&state=72du0BBFIPjW4PsWcI21SccfRX-EkojC4dZ49MYJZac", + "/golang.org/x/oauth2", + } { + assert.True(t, IsRelativeURL(u)) + } + +} diff --git a/version.go b/version.go index 1a7ff97..daee5cc 100644 --- a/version.go +++ b/version.go @@ -2,9 +2,9 @@ // go-aah/essentials source code and usage is governed by a MIT style // license that can be found in the LICENSE file. -// Package ess provides simple & useful utils for Go. aah framework utilizes -// essentials library across. It's pretty handy you can use it too :) +// Package ess provides simple & useful util functions for Go. aah framework +// utilizes essentials library across. package ess // Version no. of essentials library -var Version = "0.7" +var Version = "0.8.0"