Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
collateKvRecursive continue on unexported field (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Piegay committed Jun 21, 2016
1 parent b432e31 commit 5657817
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Stært is a Go library for loading and merging a program configuration structure

## Overview
Stært was born in order to merge two sources of Configuration ([Flæg](https://github.com/containous/flaeg), [Toml](http://github.com/BurntSushi/toml)).
We developed [`flaeg`](https://github.com/containous/flaeg) and `staert` in order to simplify configuration maintenance on [traefik](https://github.com/containous/traefik).
Now it also supports [Key-Value Store](#kvstore).
We developed [Flæg](https://github.com/containous/flaeg) and Stært in order to simplify configuration maintenance on [Træfik](https://github.com/containous/traefik).

## Features
- Load your Configuration structure from many sources
Expand Down Expand Up @@ -186,7 +187,7 @@ It handles :
Note : Hopefully, we provide the function `StoreConfig` to store your configuration structure ;)
### KvSource
KvSource impement Source:
KvSource implements Source:
```go
type KvSource struct {
Expand Down
4 changes: 4 additions & 0 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func collateKvRecursive(objValue reflect.Value, kv map[string]string, key string
case reflect.Struct:
for i := 0; i < objValue.NumField(); i++ {
objType := objValue.Type()
if objType.Field(i).Name[:1] != strings.ToUpper(objType.Field(i).Name[:1]) {
//if unexported field
continue
}
squashed := false
if objType.Field(i).Anonymous {
if objValue.Field(i).Kind() == reflect.Struct {
Expand Down
56 changes: 54 additions & 2 deletions kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func TestCollateKvPairsBasic(t *testing.T) {
Vbool: true,
Vfloat: 1.5,
Vextra: "toto",
vsilent: true,
vsilent: true, //Unexported : must not be in the map
Vdata: 42,
}
//test
Expand All @@ -753,7 +753,6 @@ func TestCollateKvPairsBasic(t *testing.T) {
"prefix/vbool": "true",
"prefix/vfloat": "1.5",
"prefix/vextra": "toto",
"prefix/vsilent": "true",
"prefix/vdata": "42",
"prefix/vstring": "tata",
"prefix/vint": "-15",
Expand Down Expand Up @@ -1084,3 +1083,56 @@ func TestStoreConfigEmbeddedSquash(t *testing.T) {
}

}

func TestCollateKvPairsUnexported(t *testing.T) {
config := &struct {
Vstring string
vsilent string
}{
Vstring: "mustBeInTheMap",
vsilent: "mustNotBeInTheMap",
}
//test
kv := map[string]string{}
if err := collateKvRecursive(reflect.ValueOf(config), kv, "prefix"); err != nil {
t.Fatalf("Error : %s", err)
}
//check
if _, ok := kv["prefix/vsilent"]; ok {
t.Fatalf("Exported field should not be in the map : %s", kv)
}

check := map[string]string{
"prefix/vstring": "mustBeInTheMap",
}
if !reflect.DeepEqual(kv, check) {
t.Fatalf("Expected %s\nGot %s", check, kv)
}

}

func TestCollateKvPairsShortNameUnexported(t *testing.T) {
config := &struct {
E string
u string
}{
E: "mustBeInTheMap",
u: "mustNotBeInTheMap",
}
//test
kv := map[string]string{}
if err := collateKvRecursive(reflect.ValueOf(config), kv, "prefix"); err != nil {
t.Fatalf("Error : %s", err)
}
//check
if _, ok := kv["prefix/u"]; ok {
t.Fatalf("Exported field should not be in the map : %s", kv)
}

check := map[string]string{
"prefix/e": "mustBeInTheMap",
}
if !reflect.DeepEqual(kv, check) {
t.Fatalf("Expected %s\nGot %s", check, kv)
}
}

0 comments on commit 5657817

Please sign in to comment.