Skip to content

Commit 53609e7

Browse files
authored
Merge pull request #8 for v0.6 Release
2 parents f636e30 + cf06313 commit 53609e7

File tree

12 files changed

+240
-12
lines changed

12 files changed

+240
-12
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ branches:
1010
- /^v[0-9]\.[0-9]/
1111

1212
go:
13-
- 1.7.x
1413
- 1.8
1514
- 1.8.x
1615
- tip

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# essentials - aah framework
22
[![Build Status](https://travis-ci.org/go-aah/essentials.svg?branch=master)](https://travis-ci.org/go-aah/essentials) [![codecov](https://codecov.io/gh/go-aah/essentials/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/essentials/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/essentials.v0)](https://goreportcard.com/report/aahframework.org/essentials.v0)
3-
[![Version](https://img.shields.io/badge/version-0.5-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE)
3+
[![Version](https://img.shields.io/badge/version-0.6-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE)
44

5-
***v0.5 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Apr 07, 2017***
5+
***v0.6 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Jul 22, 2017***
66

77
`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:
88
* filepath
@@ -24,4 +24,4 @@
2424
go get -u aahframework.org/essentials.v0
2525
```
2626

27-
See official page [TODO]
27+
Visit official website https://aahframework.org to learn more.

archive_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ess
66

77
import (
88
"io/ioutil"
9+
"strings"
910
"testing"
1011

1112
"aahframework.org/test.v0/assert"
@@ -39,4 +40,12 @@ func TestArchiveZip(t *testing.T) {
3940
err := Zip(zipName, join(testdataPath, "dirpaths"))
4041
assert.Nil(t, err)
4142
assert.True(t, IsFileExists(zipName))
43+
44+
err = Zip(zipName, join(testdataPath, "dirpaths1"))
45+
assert.NotNil(t, err)
46+
assert.True(t, strings.HasPrefix(err.Error(), "source does not exists:"))
47+
48+
err = Zip(zipName, join(testdataPath, "dirpaths"))
49+
assert.NotNil(t, err)
50+
assert.True(t, strings.HasPrefix(err.Error(), "destination archive already exists:"))
4251
}

essentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
package ess
88

99
// Version no. of essentials library
10-
var Version = "0.5"
10+
var Version = "0.6"

filepath.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,23 @@ func DeleteFiles(files ...string) (errs []error) {
269269

270270
// DirsPath method returns all directories absolute path from given base path recursively.
271271
func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
272+
return DirsPathExcludes(basePath, recursive, Excludes{})
273+
}
274+
275+
// DirsPathExcludes method returns all directories absolute path from given base path recursively
276+
// excluding the excludes list.
277+
func DirsPathExcludes(basePath string, recursive bool, excludes Excludes) (pdirs []string, err error) {
278+
if err = excludes.Validate(); err != nil {
279+
return
280+
}
281+
272282
if recursive {
273283
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
284+
if info.IsDir() && excludes.Match(filepath.Base(srcPath)) {
285+
// excluding directory
286+
return filepath.SkipDir
287+
}
288+
274289
if info.IsDir() {
275290
pdirs = append(pdirs, srcPath)
276291
}
@@ -286,7 +301,7 @@ func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
286301
}
287302

288303
for _, v := range list {
289-
if v.IsDir() {
304+
if v.IsDir() && !excludes.Match(v.Name()) {
290305
pdirs = append(pdirs, filepath.Join(basePath, v.Name()))
291306
}
292307
}
@@ -296,8 +311,23 @@ func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
296311

297312
// FilesPath method returns all files absolute path from given base path recursively.
298313
func FilesPath(basePath string, recursive bool) (files []string, err error) {
314+
return FilesPathExcludes(basePath, recursive, Excludes{})
315+
}
316+
317+
// FilesPathExcludes method returns all files absolute path from given base path recursively
318+
// excluding the excludes list.
319+
func FilesPathExcludes(basePath string, recursive bool, excludes Excludes) (files []string, err error) {
320+
if err = excludes.Validate(); err != nil {
321+
return
322+
}
323+
299324
if recursive {
300325
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
326+
if !info.IsDir() && excludes.Match(filepath.Base(srcPath)) {
327+
// excluding file
328+
return nil
329+
}
330+
301331
if !info.IsDir() {
302332
files = append(files, srcPath)
303333
}
@@ -313,7 +343,7 @@ func FilesPath(basePath string, recursive bool) (files []string, err error) {
313343
}
314344

315345
for _, v := range list {
316-
if !v.IsDir() {
346+
if !v.IsDir() && !excludes.Match(v.Name()) {
317347
files = append(files, filepath.Join(basePath, v.Name()))
318348
}
319349
}

filepath_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ func TestStripExt(t *testing.T) {
219219

220220
name3 := StripExt("")
221221
assert.Equal(t, "", name3)
222+
223+
name4 := StripExt("myname")
224+
assert.Equal(t, "myname", name4)
222225
}
223226

224227
func TestDirPaths(t *testing.T) {
@@ -250,6 +253,10 @@ func TestDirPaths(t *testing.T) {
250253
assert.True(t, IsSliceContainsString(dirs, path11))
251254
assert.True(t, IsSliceContainsString(dirs, path12))
252255
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
256+
257+
dirs, err = DirsPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"level1-2", "level2-2"})
258+
assert.FailOnError(t, err, "unable to get directory list")
259+
assert.True(t, len(dirs) == 6)
253260
}
254261

255262
func TestFilesPath(t *testing.T) {
@@ -284,4 +291,8 @@ func TestFilesPath(t *testing.T) {
284291
files, err = FilesPath(path11, false)
285292
assert.Nil(t, err)
286293
assert.True(t, strings.HasSuffix(files[0], "file11.txt"))
294+
295+
files, err = FilesPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"file12.txt", "file22.txt"})
296+
assert.Nil(t, err)
297+
assert.True(t, len(files) == 3)
287298
}

fmt_flag.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
)
11+
12+
var (
13+
// FmtFlagSeparator is used parse flags pattern.
14+
FmtFlagSeparator = "%"
15+
16+
// FmtFlagValueSeparator is used to parse into flag and value.
17+
FmtFlagValueSeparator = ":"
18+
19+
defaultFormat = "%v"
20+
)
21+
22+
type (
23+
// FmtFlagPart is indiviual flag details
24+
// For e.g.:
25+
// part := FmtFlagPart{
26+
// Flag: FmtFlagTime,
27+
// Name: "time",
28+
// Format: "2006-01-02 15:04:05.000",
29+
// }
30+
FmtFlagPart struct {
31+
Flag FmtFlag
32+
Name string
33+
Format string
34+
}
35+
36+
// FmtFlag type definition
37+
FmtFlag uint8
38+
)
39+
40+
// ParseFmtFlag it parses the given pattern, format flags into format flag parts.
41+
// For e.g.:
42+
// %time:2006-01-02 15:04:05.000 %level:-5 %message
43+
// %clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:Referer %reshdr:Server
44+
func ParseFmtFlag(pattern string, fmtFlags map[string]FmtFlag) ([]FmtFlagPart, error) {
45+
var flagParts []FmtFlagPart
46+
pattern = strings.TrimSpace(pattern)
47+
formatFlags := strings.Split(pattern, FmtFlagSeparator)[1:]
48+
for _, f := range formatFlags {
49+
f = strings.TrimSpace(f)
50+
parts := strings.SplitN(f, FmtFlagValueSeparator, 2)
51+
flag, found := fmtFlags[parts[0]]
52+
if !found {
53+
return nil, fmt.Errorf("fmtflag: unknown flag '%s'", f)
54+
}
55+
56+
part := FmtFlagPart{Flag: flag, Name: parts[0]}
57+
switch len(parts) {
58+
case 2:
59+
// handle `time` related flag, `custom` flag
60+
// and `hdr` flag particularly
61+
if strings.Contains(parts[0], "time") || parts[0] == "custom" ||
62+
strings.HasSuffix(parts[0], "hdr") {
63+
part.Format = parts[1]
64+
} else {
65+
part.Format = "%" + parts[1] + "v"
66+
}
67+
default:
68+
part.Format = defaultFormat
69+
}
70+
71+
flagParts = append(flagParts, part)
72+
}
73+
74+
return flagParts, nil
75+
}

fmt_flag_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"testing"
9+
10+
"aahframework.org/test.v0/assert"
11+
)
12+
13+
func TestFmtParseFlagLog(t *testing.T) {
14+
const (
15+
FmtFlagLevel FmtFlag = iota
16+
FmtFlagTime
17+
FmtFlagUTCTime
18+
FmtFlagLongfile
19+
FmtFlagShortfile
20+
FmtFlagLine
21+
FmtFlagMessage
22+
FmtFlagCustom
23+
)
24+
25+
logFmtFlags := map[string]FmtFlag{
26+
"level": FmtFlagLevel,
27+
"time": FmtFlagTime,
28+
"utctime": FmtFlagUTCTime,
29+
"longfile": FmtFlagLongfile,
30+
"shortfile": FmtFlagShortfile,
31+
"line": FmtFlagLine,
32+
"message": FmtFlagMessage,
33+
"custom": FmtFlagCustom,
34+
}
35+
36+
flagParts, err := ParseFmtFlag("%time:2006-01-02 15:04:05.000 %level %custom:- %message", logFmtFlags)
37+
assert.Nil(t, err)
38+
39+
assertFlagPart(t, "time", "2006-01-02 15:04:05.000", FmtFlag(1), flagParts[0])
40+
assertFlagPart(t, "level", "%v", FmtFlag(0), flagParts[1])
41+
assertFlagPart(t, "custom", "-", FmtFlag(7), flagParts[2])
42+
assertFlagPart(t, "message", "%v", FmtFlag(6), flagParts[3])
43+
44+
// Unknown flag
45+
flagParts, err = ParseFmtFlag("%myflag", logFmtFlags)
46+
assert.NotNil(t, err)
47+
assert.Equal(t, "fmtflag: unknown flag 'myflag'", err.Error())
48+
assert.True(t, len(flagParts) == 0)
49+
}
50+
51+
func TestFmtParseFlagAccessLog(t *testing.T) {
52+
const (
53+
fmtFlagClientIP FmtFlag = iota
54+
fmtFlagRequestTime
55+
fmtFlagRequestURL
56+
fmtFlagRequestMethod
57+
fmtFlagRequestID
58+
fmtFlagRequestHeader
59+
fmtFlagQueryString
60+
fmtFlagResponseStatus
61+
fmtFlagResponseSize
62+
fmtFlagResponseHeader
63+
fmtFlagResponseTime
64+
)
65+
66+
accessLogFmtFlags := map[string]FmtFlag{
67+
"clientip": fmtFlagClientIP,
68+
"reqtime": fmtFlagRequestTime,
69+
"requrl": fmtFlagRequestURL,
70+
"reqmethod": fmtFlagRequestMethod,
71+
"reqid": fmtFlagRequestID,
72+
"reqhdr": fmtFlagRequestHeader,
73+
"querystr": fmtFlagQueryString,
74+
"resstatus": fmtFlagResponseStatus,
75+
"ressize": fmtFlagResponseSize,
76+
"reshdr": fmtFlagResponseHeader,
77+
"restime": fmtFlagResponseTime,
78+
}
79+
80+
flagParts, err := ParseFmtFlag("%clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:Referer %reshdr:Server", accessLogFmtFlags)
81+
assert.Nil(t, err)
82+
83+
assertFlagPart(t, "clientip", "%v", FmtFlag(0), flagParts[0])
84+
assertFlagPart(t, "reqid", "%v", FmtFlag(4), flagParts[1])
85+
assertFlagPart(t, "reqtime", "%v", FmtFlag(1), flagParts[2])
86+
assertFlagPart(t, "restime", "%v", FmtFlag(10), flagParts[3])
87+
assertFlagPart(t, "resstatus", "%v", FmtFlag(7), flagParts[4])
88+
assertFlagPart(t, "ressize", "%v", FmtFlag(8), flagParts[5])
89+
assertFlagPart(t, "reqmethod", "%v", FmtFlag(3), flagParts[6])
90+
assertFlagPart(t, "requrl", "%v", FmtFlag(2), flagParts[7])
91+
assertFlagPart(t, "reqhdr", "Referer", FmtFlag(5), flagParts[8])
92+
assertFlagPart(t, "reshdr", "Server", FmtFlag(9), flagParts[9])
93+
}
94+
95+
func assertFlagPart(t *testing.T, name, format string, fflag FmtFlag, flagPart FmtFlagPart) {
96+
t.Logf("Fmt Flag: %v", format)
97+
assert.Equal(t, name, flagPart.Name)
98+
assert.Equal(t, format, flagPart.Format)
99+
assert.Equal(t, fflag, flagPart.Flag)
100+
}

guid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535
)
3636

3737
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
38-
// Global methods
38+
// Package methods
3939
//___________________________________
4040

4141
// NewGUID method returns a new Globally Unique identifier (GUID).

guid_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ type guidParts struct {
5454
}
5555

5656
var uniqueIds = []guidParts{
57-
guidParts{
57+
{
5858
"4d88e15b60f486e428412dc9",
5959
1300816219,
6060
[]byte{0x60, 0xf4, 0x86},
6161
0xe428,
6262
4271561,
6363
},
64-
guidParts{
64+
{
6565
"000000000000000000000000",
6666
0,
6767
[]byte{0x00, 0x00, 0x00},
6868
0x0000,
6969
0,
7070
},
71-
guidParts{
71+
{
7272
"00000000aabbccddee000001",
7373
0,
7474
[]byte{0xaa, 0xbb, 0xcc},

0 commit comments

Comments
 (0)