Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Resolve race conditions, panics, and other errors #1

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
jobs:
build:
docker:
- image: circleci/golang:1.12
environment:
- GO111MODULE=on
working_directory: /go/src/github.com/ory/viper
steps:
- checkout
- run: go test -race -v ./...
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go_import_path: github.com/spf13/viper
go_import_path: github.com/ory/viper

language: go

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SHELL=/bin/bash -o pipefail

# Formats the code
.PHONY: format
format:
goreturns -w -local github.com/ory $$(listx .)
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
![viper logo](https://cloud.githubusercontent.com/assets/173412/10886745/998df88a-8151-11e5-9448-4736db51020d.png)

[![CircleCI](https://circleci.com/gh/ory/viper/tree/master.svg?style=shield](https://circleci.com/gh/ory/viper/tree/master)

Go configuration with fangs!

> This is a fork. It resolves several issues that are left unresolved in [the upstream](https://github.com/ory/viper).
> Issues resolved and features added include:
>
> - Fixed race conditions when reloading configs.
> - Added `HasChanged(key string) bool` which returns true (once!) when a value has changed.
> - Make sure that `viper.AllSettings()` always returns `map[string]interface{}` which was not the case and incompatible
with de/encoders like `json`.

Many Go projects are built using Viper including:

* [Hugo](http://gohugo.io)
Expand All @@ -13,11 +23,11 @@ Many Go projects are built using Viper including:
* [doctl](https://github.com/digitalocean/doctl)
* [Clairctl](https://github.com/jgsqware/clairctl)

[![Build Status](https://travis-ci.org/spf13/viper.svg)](https://travis-ci.org/spf13/viper) [![Join the chat at https://gitter.im/spf13/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/spf13/viper?status.svg)](https://godoc.org/github.com/spf13/viper)
[![Build Status](https://travis-ci.org/ory/viper.svg)](https://travis-ci.org/ory/viper) [![Join the chat at https://gitter.im/ory/viper](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ory/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![GoDoc](https://godoc.org/github.com/ory/viper?status.svg)](https://godoc.org/github.com/ory/viper)

## Install
```console
go get -u github.com/spf13/viper
go get -u github.com/ory/viper
```

## What is Viper?
Expand Down Expand Up @@ -384,7 +394,7 @@ viper.BindFlagValues("my-flags", fSet)
To enable remote support in Viper, do a blank import of the `viper/remote`
package:

`import _ "github.com/spf13/viper/remote"`
`import _ "github.com/ory/viper/remote"`

Viper will read a config string (as JSON, TOML, YAML, HCL or envfile) retrieved from a path
in a Key/Value store such as etcd or Consul. These values take precedence over
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/spf13/viper
module github.com/ory/viper

require (
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
Expand Down Expand Up @@ -28,6 +28,7 @@ require (
github.com/spf13/jwalterweatherman v1.0.0
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
github.com/subosito/gotenv v1.1.1
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
github.com/ugorji/go v1.1.4 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down Expand Up @@ -117,6 +115,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/subosito/gotenv v1.1.1 h1:TWxckSF6WVKWbo2M3tMqCtWa9NFUgqM1SSynxmYONOI=
github.com/subosito/gotenv v1.1.1/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
Expand Down
3 changes: 2 additions & 1 deletion remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"io"
"os"

"github.com/spf13/viper"
crypt "github.com/xordataexchange/crypt/config"

"github.com/ory/viper"
)

type remoteConfigProvider struct{}
Expand Down
12 changes: 12 additions & 0 deletions stub/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"foo": {
"bar": [
{
"baz": 1
},
{
"baz": 2
}
]
}
}
4 changes: 4 additions & 0 deletions stub/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo:
bar:
- baz: 1
- baz: 2
25 changes: 25 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,28 @@ func deepSearch(m map[string]interface{}, path []string) map[string]interface{}
}
return m
}

// toMapStringInterface is a workaround for https://github.com/ory/viper/issues/730
// and https://github.com/go-yaml/yaml/issues/139
func toMapStringInterface(in interface{}) interface{} {
switch t := in.(type) {
case map[string]interface{}:
for k, v := range t {
t[k] = toMapStringInterface(v)
}
return t
case map[interface{}]interface{}:
nt := make(map[string]interface{})
for k, v := range t {
nt[fmt.Sprintf("%s", k)] = toMapStringInterface(v)
}
return nt
case []interface{}:
for k, v := range t {
t[k] = toMapStringInterface(v)
}
return t
default:
return in
}
}
20 changes: 20 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package viper
import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCopyAndInsensitiviseMap(t *testing.T) {
Expand Down Expand Up @@ -52,3 +54,21 @@ func TestCopyAndInsensitiviseMap(t *testing.T) {
t.Fatal("Input map changed")
}
}

func TestToMapStringInterface(t *testing.T) {
assert.EqualValues(
t,
map[string]interface{}{
"foo": "bar",
"items": map[string]interface{}{
"foo": "bar",
},
},
toMapStringInterface(map[string]interface{}{
"foo": "bar",
"items": map[interface{}]interface{}{
"foo": "bar",
},
}),
)
}