Passed
Push — master ( ad64d3...e465b8 )
by Serhii
01:08 queued 11s
created

timeago.parseJsonIntoLang   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 7
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"io/ioutil"
7
	"log"
8
	"os"
9
	"strconv"
10
	"time"
11
)
12
13
func fileExists(filePath string) (bool, error) {
14 1
	_, err := os.Stat(filePath)
15
16 1
	if err == nil {
17 1
		return true, nil
18
	}
19
20 1
	if errors.Is(err, os.ErrNotExist) {
21 1
		return false, nil
22
	}
23
24
	return false, err
25
}
26
27
func getFileContent(filePath string) []byte {
28 1
	content, err := ioutil.ReadFile(filePath)
29
30 1
	if err != nil {
31
		log.Fatalln(err)
32
	}
33
34 1
	return content
35
}
36
37
func getTimestampOfPastDate(subDuration time.Duration) int {
38 1
	return int(time.Now().Add(-subDuration).UnixNano() / 1000000000)
39
}
40
41
func parseTimestampToTime(timestamp int) time.Time {
42 1
	return time.Unix(int64(timestamp), 0)
43
}
44
45
func getLastNumberDigit(num int) int {
46 1
	numStr := strconv.Itoa(num)
47 1
	result, _ := strconv.Atoi(numStr[len(numStr)-1:])
48
49 1
	return result
50
}
51
52
func parseJsonIntoLang(fileName string) lang {
53 1
	content := getFileContent(fileName)
54
55 1
	var result lang
56
57 1
	err := json.Unmarshal(content, &result)
58
59 1
	if err != nil {
60
		log.Fatalln(err)
61
	}
62
63 1
	return result
64
}
65