Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from Netflix:master #103

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1144160
support time.Duration
kitt1987 May 12, 2020
5660fe1
Merge pull request #7 from git-roll/support-duration
hinshun May 12, 2020
fa034df
Add default value option in env tag
Jul 30, 2020
4b25b37
add default value example to readme
Jul 30, 2020
8b9a491
Use default= instead of || to specify default vals
jimmykodes Aug 3, 2020
9271595
Merge pull request #8 from jimmykodes/add_default_values
hinshun Aug 3, 2020
ded87df
Split default value only into two parts
steveteuber Sep 8, 2020
3e802f6
Merge pull request #9 from steveteuber/key-value
hinshun Sep 8, 2020
579d7ee
Update build status location
sghill Nov 3, 2020
014a952
Merge pull request #11 from sghill/travis-ci-com
hinshun Nov 3, 2020
1248347
Add support for custom env unmarshaling
mothershipper Nov 25, 2020
260a4dc
Adds custom marshaler interface and handling
mothershipper Nov 26, 2020
fc6c08c
Adds tests for values in custom (un)marshaler
mothershipper Nov 26, 2020
0920e6e
Add float32 and float64 support
kplachkov Dec 23, 2020
48dd879
Merge pull request #14 from kplachkov/feature/float_support
hinshun Dec 24, 2020
e4d8597
Merge pull request #12 from mothership/master
hinshun Dec 24, 2020
e5bd531
Fix tiny typo in test
seawolf Jan 16, 2021
8f74e74
Merge pull request #16 from seawolf/patch-1
hinshun Jan 16, 2021
36a1c27
:adhesive_bandage: Add detail for required field error
PePoDev Feb 15, 2021
40fa93b
:adhesive_bandage: Refactor error type
PePoDev Feb 15, 2021
e437a7e
Merge pull request #17 from PePoDev/master
hinshun Feb 15, 2021
3394e7c
Add uint uint8 uint16 uint32 uint64 support
May 24, 2022
5fdd701
adding more tests for uint support
May 24, 2022
78278af
Merge pull request #22 from jorgejr568/master
coryb May 26, 2022
4bce73d
Make repo a Go module, switch to GitHub Actions (#25)
patricksanders Aug 6, 2024
e66d55a
Remove go minor revision specification (#26)
francesconi Aug 29, 2024
131cef9
Slice support (#13)
jimmykodes Aug 29, 2024
ec74a1d
chore: go 1.23 upgrade, README fix, docstring additions, & go styling…
sheikhrachel Oct 25, 2024
682abba
Add an example for a array (#29)
flemming-pr Oct 25, 2024
6b7f898
fix: Marshal() including "default=" tags in EnvSet (#27)
kaandesu Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

86 changes: 80 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# go-env

[![Build Status](https://travis-ci.org/Netflix/go-env.svg?branch=master)](https://travis-ci.org/Netflix/go-env)
[![GoDoc](https://godoc.org/github.com/Netflix/go-env?status.svg)](https://godoc.org/github.com/Netflix/go-env)
![Build Status](https://github.com/Netflix/go-env/actions/workflows/build.yml/badge.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/Netflix/go-env.svg)](https://pkg.go.dev/github.com/Netflix/go-env)
[![NetflixOSS Lifecycle](https://img.shields.io/osslifecycle/Netflix/go-expect.svg)]()


Expand All @@ -14,8 +14,9 @@ package main

import (
"log"
"time"

env "github.com/Netflix/go-env"
"github.com/Netflix/go-env"
)

type Environment struct {
Expand All @@ -32,6 +33,11 @@ type Environment struct {
}

Extras env.EnvSet

Duration time.Duration `env:"TYPE_DURATION"`
DefaultValue string `env:"MISSING_VAR,default=default_value"`
RequiredValue string `env:"IM_REQUIRED,required=true"`
ArrayValue []string `env:"ARRAY_VALUE,default=value1|value2|value3"`
}

func main() {
Expand All @@ -45,7 +51,7 @@ func main() {

// ...

es, err = env.Marshal(environment)
es, err = env.Marshal(&environment)
if err != nil {
log.Fatal(err)
}
Expand All @@ -59,11 +65,79 @@ func main() {
es.Apply(cs)

environment = Environment{}
err = env.Unmarshal(es, &environment)
if err != nil {
if err = env.Unmarshal(es, &environment); err != nil {
log.Fatal(err)
}

environment.Extras = es
}
```

This will initially throw an error if `IM_REQUIRED` is not set in the environment as part of the env struct validation.

This error can be resolved by setting the `IM_REQUIRED` environment variable manually in the environment or by setting it in the
code prior to calling `UnmarshalFromEnviron` with:
```go
os.Setenv("IM_REQUIRED", "some_value")
```

## Custom Marshaler/Unmarshaler

There is limited support for dictating how a field should be marshaled or unmarshaled. The following example
shows how you could marshal/unmarshal from JSON

```go
package main

import (
"encoding/json"
"fmt"
"log"

"github.com/Netflix/go-env"
)

type SomeData struct {
SomeField int `json:"someField"`
}

func (s *SomeData) UnmarshalEnvironmentValue(data string) error {
var tmp SomeData
if err := json.Unmarshal([]byte(data), &tmp); err != nil {
return err
}
*s = tmp
return nil
}

func (s SomeData) MarshalEnvironmentValue() (string, error) {
bytes, err := json.Marshal(s)
if err != nil {
return "", err
}
return string(bytes), nil
}

type Config struct {
SomeData *SomeData `env:"SOME_DATA"`
}

func main() {
var cfg Config
if _, err := env.UnmarshalFromEnviron(&cfg); err != nil {
log.Fatal(err)
}

if cfg.SomeData != nil && cfg.SomeData.SomeField == 42 {
fmt.Println("Got 42!")
} else {
fmt.Printf("Got nil or some other value: %v\n", cfg.SomeData)
}

es, err := env.Marshal(&cfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Got the following: %+v\n", es)
}
```
Loading