Skip to content

Commit

Permalink
feat: Migrate to generics usage (#53)
Browse files Browse the repository at this point in the history
* feat: Implement generic function EnvOrDefault

* chore: Bump GOTOOLS_IMAGE_TAG to v0.1.4

* refactor: Scripts and makefile

* fix: Add git safe directory

* refactor: Move tests to package layer

* docs: Update readme coverage

* ci: Fix pipeline

* chore: Remove //nolint directive

* refactor: Make parameters not optional for internal parser

* chore: Add cases for wrong and empty separators in slice parsers

* fix: Empty separator early return
  • Loading branch information
obalunenko authored Jan 30, 2023
1 parent 2d19c8e commit 8b67b00
Show file tree
Hide file tree
Showing 17 changed files with 969 additions and 142 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,9 @@ jobs:
make install-tools
shell: bash

- name: Prepare test coverage
- name: Prepare test coverage and reports
run: |
make test-cover
shell: bash

- name: Tests report
run: |
make test-sonar-report
make prepare-cover-report
shell: bash

- name: Prepare lint report
Expand Down
17 changes: 8 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ VERSION ?= $(shell git describe --tags $(git rev-list --tags --max-count=1))
APP_NAME?=getenv
SHELL := env APP_NAME=$(APP_NAME) $(SHELL)

GOTOOLS_IMAGE_TAG?=v0.0.2
GOTOOLS_IMAGE_TAG?=v0.1.4
SHELL := env GOTOOLS_IMAGE_TAG=$(GOTOOLS_IMAGE_TAG) $(SHELL)

COMPOSE_TOOLS_FILE=deployments/docker-compose/go-tools-docker-compose.yml
Expand Down Expand Up @@ -36,22 +36,21 @@ help:


## Test coverage report.
test-cover: vet
$(COMPOSE_TOOLS_CMD_UP) run-tests-coverage run-tests-coverage
test-cover:
./scripts/tests/coverage.sh
.PHONY: test-cover

## Tests sonar report generate.
test-sonar-report:
./scripts/tests/sonar-report.sh
.PHONY: test-sonar-report
prepare-cover-report: test-cover
$(COMPOSE_TOOLS_CMD_UP) prepare-cover-report prepare-cover-report
.PHONY: prepare-cover-report

## Open coverage report.
open-cover-report: test-cover
open-cover-report: prepare-cover-report
./scripts/open-coverage-report.sh
.PHONY: open-cover-report

## Update readme coverage.
update-readme-cover: test-cover
update-readme-cover: prepare-cover-report
$(COMPOSE_TOOLS_CMD_UP) update-readme-coverage update-readme-coverage
.PHONY: update-readme-cover

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-96.23%25-brightgreen?longCache=true&style=flat)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-100%25-brightgreen?longCache=true&style=flat)


![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/obalunenko/getenv)
Expand Down
9 changes: 7 additions & 2 deletions deployments/docker-compose/go-tools-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ services:
service: tools
entrypoint: /bin/sh -c './scripts/tests/coverage.sh'

prepare-cover-report:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/tests/prepare-cover-report.sh'

update-readme-coverage:
extends:
service: tools
Expand All @@ -40,9 +45,9 @@ services:
lint-pipeline:
extends:
service: tools
entrypoint: /bin/sh -c './scripts/linting/golangci-pipeline.sh'
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-pipeline.sh'

lint-sonar:
extends:
service: tools
entrypoint: /bin/sh -c './scripts/linting/golangci-sonar.sh'
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-sonar.sh'
161 changes: 73 additions & 88 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,145 +2,130 @@
package getenv

import (
"os"
"strconv"
"strings"
"time"

"github.com/obalunenko/getenv/internal"
"github.com/obalunenko/getenv/option"
)

// IntOrDefault retrieves the int value of the environment variable named
// EnvOrDefault retrieves the value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func IntOrDefault(key string, defaultVal int) int {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}
func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T {
w := internal.NewEnvParser(defaultVal)

params := newParseParams(options)

val := w.ParseEnv(key, defaultVal, params)

val, err := strconv.Atoi(env)
if err != nil {
return defaultVal
return val.(T)
}

func newParseParams(opts []option.Option) internal.Parameters {
var p internal.Parameters

for _, opt := range opts {
opt.Apply(&p)
}

return val
return p
}

// IntOrDefault retrieves the int value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func IntOrDefault(key string, defaultVal int) int {
return EnvOrDefault(key, defaultVal)
}

// StringOrDefault retrieves the string value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func StringOrDefault(key, defaultVal string) string {
env, ok := os.LookupEnv(key)
if !ok || env == "" {
return defaultVal
}

return env
return EnvOrDefault(key, defaultVal)
}

// BoolOrDefault retrieves the bool value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func BoolOrDefault(key string, defaultVal bool) bool {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}

val, err := strconv.ParseBool(env)
if err != nil {
return defaultVal
}

return val
return EnvOrDefault(key, defaultVal)
}

// StringSliceOrDefault retrieves the string slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func StringSliceOrDefault(key string, defaultVal []string, sep string) []string {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}
return EnvOrDefault(key, defaultVal, option.WithSeparator(sep))
}

val := strings.Split(env, sep)
// IntSliceOrDefault retrieves the int slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func IntSliceOrDefault(key string, defaultVal []int, sep string) []int {
return EnvOrDefault(key, defaultVal, option.WithSeparator(sep))
}

return val
// Float64SliceOrDefault retrieves the float64 slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func Float64SliceOrDefault(key string, defaultVal []float64, sep string) []float64 {
return EnvOrDefault(key, defaultVal, option.WithSeparator(sep))
}

// DurationOrDefault retrieves the time.Duration value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func DurationOrDefault(key string, defaultVal time.Duration) time.Duration {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}

val, err := time.ParseDuration(env)
if err != nil {
return defaultVal
}

return val
return EnvOrDefault(key, defaultVal)
}

// TimeOrDefault retrieves the time.Time value of the environment variable named
// by the key represented by layout.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func TimeOrDefault(key string, defaultVal time.Time, layout string) time.Time {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}

val, err := time.Parse(layout, env)
if err != nil {
return defaultVal
}

return val
return EnvOrDefault(key, defaultVal, option.WithTimeLayout(layout))
}

// Int64OrDefault retrieves the int64 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func Int64OrDefault(key string, defaultVal int64) int64 {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
bitsize = 64
)

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return val
return EnvOrDefault(key, defaultVal)
}

// Float64OrDefault retrieves the float64 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func Float64OrDefault(key string, defaultVal float64) float64 {
env := StringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
bitsize = 64
)

val, err := strconv.ParseFloat(env, bitsize)
if err != nil {
return defaultVal
}
return EnvOrDefault(key, defaultVal)
}

return val
// Int64SliceOrDefault retrieves the int6464 slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
//
// Deprecated: use EnvOrDefault.
func Int64SliceOrDefault(key string, defaultVal []int64, sep string) []int64 {
return EnvOrDefault(key, defaultVal, option.WithSeparator(sep))
}
Loading

0 comments on commit 8b67b00

Please sign in to comment.