mirror of
https://github.com/spf13/viper
synced 2025-05-07 20:57:18 +00:00
Merge pull request #7 from ShaleApps/undo-changes-and-sync-upstream
Undo changes and sync upstream
This commit is contained in:
commit
a762f8a704
72 changed files with 5613 additions and 1076 deletions
15
.editorconfig
Normal file
15
.editorconfig
Normal file
|
@ -0,0 +1,15 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
|
||||
[{Makefile,*.mk}]
|
||||
indent_style = tab
|
2
.github/.editorconfig
vendored
Normal file
2
.github/.editorconfig
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
[{*.yml,*.yaml}]
|
||||
indent_size = 2
|
119
.github/ISSUE_TEMPLATE/bug_report.yaml
vendored
Normal file
119
.github/ISSUE_TEMPLATE/bug_report.yaml
vendored
Normal file
|
@ -0,0 +1,119 @@
|
|||
name: 🐛 Bug report
|
||||
description: Report a bug to help us improve Viper
|
||||
labels: [kind/bug]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for submitting a bug report!
|
||||
|
||||
Please fill out the template below to make it easier to debug your problem.
|
||||
|
||||
If you are not sure if it is a bug or not, you can contact us via the available [support channels](https://github.com/spf13/viper/issues/new/choose).
|
||||
- type: checkboxes
|
||||
attributes:
|
||||
label: Preflight Checklist
|
||||
description: Please ensure you've completed all of the following.
|
||||
options:
|
||||
- label: I have searched the [issue tracker](https://www.github.com/spf13/viper/issues) for an issue that matches the one I want to file, without success.
|
||||
required: true
|
||||
- label: I am not looking for support or already pursued the available [support channels](https://github.com/spf13/viper/issues/new/choose) without success.
|
||||
required: true
|
||||
- label: I have checked the [troubleshooting guide](https://github.com/spf13/viper/blob/master/TROUBLESHOOTING.md) for my problem, without success.
|
||||
required: true
|
||||
- type: input
|
||||
attributes:
|
||||
label: Viper Version
|
||||
description: What version of Viper are you using?
|
||||
placeholder: 1.8.1
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
attributes:
|
||||
label: Go Version
|
||||
description: What version of Go are you using?
|
||||
placeholder: "1.16"
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
attributes:
|
||||
label: Config Source
|
||||
description: What sources do you load configuration from?
|
||||
options:
|
||||
- Manual set
|
||||
- Flags
|
||||
- Environment variables
|
||||
- Files
|
||||
- Remove K/V stores
|
||||
- Defaults
|
||||
multiple: true
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
attributes:
|
||||
label: Format
|
||||
description: Which file formats do you use?
|
||||
options:
|
||||
- JSON
|
||||
- YAML
|
||||
- TOML
|
||||
- Dotenv
|
||||
- HCL
|
||||
- Java properties
|
||||
- INI
|
||||
- Other (specify below)
|
||||
multiple: true
|
||||
- type: input
|
||||
attributes:
|
||||
label: Repl.it link
|
||||
description: Complete example on Repl.it reproducing the issue. [Here](https://repl.it/@sagikazarmark/Viper-example) is an example you can use.
|
||||
placeholder: https://repl.it/@sagikazarmark/Viper-example
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Code reproducing the issue
|
||||
description: Please provide a Repl.it link if possible.
|
||||
render: go
|
||||
placeholder: |
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := viper.New()
|
||||
|
||||
// ...
|
||||
|
||||
var config Config
|
||||
|
||||
err = v.Unmarshal(&config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Expected Behavior
|
||||
description: A clear and concise description of what you expected to happen.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Actual Behavior
|
||||
description: A clear description of what actually happens.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Steps To Reproduce
|
||||
description: Steps to reproduce the behavior if it is not self-explanatory.
|
||||
placeholder: |
|
||||
1. In this environment...
|
||||
2. With this config...
|
||||
3. Run '...'
|
||||
4. See error...
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Additional Information
|
||||
description: Links? References? Anything that will give us more context about the issue that you are encountering!
|
13
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
13
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: ❓ Ask a question
|
||||
url: https://github.com/spf13/viper/discussions/new?category=q-a
|
||||
about: Ask and discuss questions with other Viper community members
|
||||
|
||||
- name: 📓 Reference
|
||||
url: https://pkg.go.dev/mod/github.com/spf13/viper
|
||||
about: Check the Go code reference
|
||||
|
||||
- name: 💬 Slack channel
|
||||
url: https://gophers.slack.com/messages/viper
|
||||
about: Please ask and answer questions here
|
39
.github/ISSUE_TEMPLATE/feature_request.yaml
vendored
Normal file
39
.github/ISSUE_TEMPLATE/feature_request.yaml
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
name: 🎉 Feature request
|
||||
description: Suggest an idea for Viper
|
||||
labels: [kind/enhancement]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for submitting a feature request!
|
||||
|
||||
Please describe what you would like to change/add and why in detail by filling out the template below.
|
||||
|
||||
If you are not sure if your request fits into Viper, you can contact us via the available [support channels](https://github.com/spf13/viper/issues/new/choose).
|
||||
- type: checkboxes
|
||||
attributes:
|
||||
label: Preflight Checklist
|
||||
description: Please ensure you've completed all of the following.
|
||||
options:
|
||||
- label: I have searched the [issue tracker](https://www.github.com/spf13/viper/issues) for an issue that matches the one I want to file, without success.
|
||||
required: true
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Problem Description
|
||||
description: A clear and concise description of the problem you are seeking to solve with this feature request.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Proposed Solution
|
||||
description: A clear and concise description of what would you like to happen.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Alternatives Considered
|
||||
description: A clear and concise description of any alternative solutions or features you've considered.
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Additional Information
|
||||
description: Add any other context about the problem here.
|
20
.github/PULL_REQUEST_TEMPLATES.md
vendored
Normal file
20
.github/PULL_REQUEST_TEMPLATES.md
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
<!--
|
||||
Thank you for sending a pull request! Here some tips for contributors:
|
||||
|
||||
1. Fill the description template below.
|
||||
2. Include appropriate tests (if necessary). Make sure that all CI checks passed.
|
||||
3. If the Pull Request is a work in progress, make use of GitHub's "Draft PR" feature and mark it as such.
|
||||
-->
|
||||
|
||||
**Overview**:
|
||||
<!-- Describe your changes briefly here. -->
|
||||
|
||||
**What problem does it solve?**:
|
||||
<!--
|
||||
- Please state in detail why we need this PR and what it solves.
|
||||
- If your PR closes some of the existing issues, please add links to them here.
|
||||
Mentioned issues will be automatically closed.
|
||||
Usage: "Closes #<issue number>", or "Closes (paste link of issue)"
|
||||
-->
|
||||
|
||||
**Special notes for a reviewer**:
|
16
.github/dependabot.yml
vendored
Normal file
16
.github/dependabot.yml
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
version: 2
|
||||
|
||||
updates:
|
||||
- package-ecosystem: gomod
|
||||
directory: /
|
||||
labels:
|
||||
- area/dependencies
|
||||
schedule:
|
||||
interval: daily
|
||||
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
labels:
|
||||
- area/dependencies
|
||||
schedule:
|
||||
interval: daily
|
BIN
.github/logo.png
vendored
Normal file
BIN
.github/logo.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
30
.github/release.yml
vendored
Normal file
30
.github/release.yml
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
changelog:
|
||||
exclude:
|
||||
labels:
|
||||
- release-note/ignore
|
||||
categories:
|
||||
- title: Exciting New Features 🎉
|
||||
labels:
|
||||
- kind/feature
|
||||
- release-note/new-feature
|
||||
- title: Enhancements 🚀
|
||||
labels:
|
||||
- kind/enhancement
|
||||
- release-note/enhancement
|
||||
- title: Bug Fixes 🐛
|
||||
labels:
|
||||
- kind/bug
|
||||
- release-note/bug-fix
|
||||
- title: Breaking Changes 🛠
|
||||
labels:
|
||||
- release-note/breaking-change
|
||||
- title: Deprecations ❌
|
||||
labels:
|
||||
- release-note/deprecation
|
||||
- title: Dependency Updates ⬆️
|
||||
labels:
|
||||
- area/dependencies
|
||||
- release-note/dependency-update
|
||||
- title: Other Changes
|
||||
labels:
|
||||
- "*"
|
2
.github/workflows/.editorconfig
vendored
2
.github/workflows/.editorconfig
vendored
|
@ -1,2 +0,0 @@
|
|||
[*.yml]
|
||||
indent_size = 2
|
18
.github/workflows/checks.yaml
vendored
Normal file
18
.github/workflows/checks.yaml
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
name: PR Checks
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, labeled, unlabeled, synchronize]
|
||||
|
||||
jobs:
|
||||
release-label:
|
||||
name: Release note label
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Check minimum labels
|
||||
uses: mheap/github-action-required-labels@v3
|
||||
with:
|
||||
mode: minimum
|
||||
count: 1
|
||||
labels: "release-note/ignore, kind/feature, release-note/new-feature, kind/enhancement, release-note/enhancement, kind/bug, release-note/bug-fix, release-note/breaking-change, release-note/deprecation, area/dependencies, release-note/dependency-update"
|
85
.github/workflows/ci.yaml
vendored
Normal file
85
.github/workflows/ci.yaml
vendored
Normal file
|
@ -0,0 +1,85 @@
|
|||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- goos: js
|
||||
goarch: wasm
|
||||
- goos: aix
|
||||
goarch: ppc64
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Build
|
||||
run: go build .
|
||||
env:
|
||||
GOOS: ${{ matrix.goos }}
|
||||
GOARCH: ${{ matrix.goarch }}
|
||||
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
# Fail fast is disabled because there are Go version specific features and tests
|
||||
# that should be able to fail independently.
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
go: ['1.17', '1.18', '1.19']
|
||||
env:
|
||||
GOFLAGS: -mod=readonly
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Test
|
||||
run: go test -race -v ./...
|
||||
if: runner.os != 'Windows'
|
||||
|
||||
- name: Test (without race detector)
|
||||
run: go test -v ./...
|
||||
if: runner.os == 'Windows'
|
||||
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
GOFLAGS: -mod=readonly
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Lint
|
||||
uses: golangci/golangci-lint-action@v3
|
||||
with:
|
||||
version: v1.50.1
|
35
.github/workflows/ci.yml
vendored
35
.github/workflows/ci.yml
vendored
|
@ -1,35 +0,0 @@
|
|||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
max-parallel: 10
|
||||
matrix:
|
||||
go: ['1.11', '1.12', '1.13']
|
||||
env:
|
||||
VERBOSE: 1
|
||||
GOFLAGS: -mod=readonly
|
||||
GOPROXY: https://proxy.golang.org
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v1
|
||||
|
||||
- name: Run tests
|
||||
run: make test
|
||||
|
||||
- name: Run linter
|
||||
run: make lint
|
72
.github/workflows/codeql-analysis.yaml
vendored
Normal file
72
.github/workflows/codeql-analysis.yaml
vendored
Normal file
|
@ -0,0 +1,72 @@
|
|||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master ]
|
||||
schedule:
|
||||
- cron: '22 16 * * 3'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'go' ]
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||
# Learn more:
|
||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
|
32
.github/workflows/feedback_issue.yaml
vendored
Normal file
32
.github/workflows/feedback_issue.yaml
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
on:
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@v6
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: `👋 Thanks for reporting!
|
||||
|
||||
A maintainer will take a look at your issue shortly. 👀
|
||||
|
||||
In the meantime: We are working on **Viper v2** and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.
|
||||
|
||||
⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9
|
||||
|
||||
📣 If you've already given us your feedback, you can still help by spreading the news,
|
||||
either by sharing the above link or telling people about this on Twitter:
|
||||
|
||||
https://twitter.com/sagikazarmark/status/1306904078967074816
|
||||
|
||||
**Thank you!** ❤️
|
||||
`,
|
||||
})
|
32
.github/workflows/feedback_pull_request.yaml
vendored
Normal file
32
.github/workflows/feedback_pull_request.yaml
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
on:
|
||||
pull_request_target:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/github-script@v6
|
||||
with:
|
||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||
script: |
|
||||
github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: `👋 Thanks for contributing to Viper! You are awesome! 🎉
|
||||
|
||||
A maintainer will take a look at your pull request shortly. 👀
|
||||
|
||||
In the meantime: We are working on **Viper v2** and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.
|
||||
|
||||
⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9
|
||||
|
||||
📣 If you've already given us your feedback, you can still help by spreading the news,
|
||||
either by sharing the above link or telling people about this on Twitter:
|
||||
|
||||
https://twitter.com/sagikazarmark/status/1306904078967074816
|
||||
|
||||
**Thank you!** ❤️
|
||||
`,
|
||||
})
|
17
.gitignore
vendored
17
.gitignore
vendored
|
@ -1,20 +1,5 @@
|
|||
/.idea/
|
||||
/bin/
|
||||
/build/
|
||||
/var/
|
||||
/vendor/
|
||||
|
||||
# IDE integration
|
||||
/.vscode/*
|
||||
!/.vscode/launch.json
|
||||
!/.vscode/tasks.json
|
||||
/.idea/*
|
||||
!/.idea/codeStyles/
|
||||
!/.idea/copyright/
|
||||
!/.idea/dataSources.xml
|
||||
!/.idea/*.iml
|
||||
!/.idea/externalDependencies.xml
|
||||
!/.idea/go.imports.xml
|
||||
!/.idea/modules.xml
|
||||
!/.idea/runConfigurations/
|
||||
!/.idea/scopes/
|
||||
!/.idea/sqldialects.xml
|
||||
|
|
96
.golangci.yaml
Normal file
96
.golangci.yaml
Normal file
|
@ -0,0 +1,96 @@
|
|||
run:
|
||||
timeout: 5m
|
||||
|
||||
linters-settings:
|
||||
gci:
|
||||
sections:
|
||||
- standard
|
||||
- default
|
||||
- prefix(github.com/spf13/viper)
|
||||
golint:
|
||||
min-confidence: 0
|
||||
goimports:
|
||||
local-prefixes: github.com/spf13/viper
|
||||
|
||||
linters:
|
||||
disable-all: true
|
||||
enable:
|
||||
- bodyclose
|
||||
- deadcode
|
||||
- dogsled
|
||||
- dupl
|
||||
- durationcheck
|
||||
- exhaustive
|
||||
- exportloopref
|
||||
- gci
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goimports
|
||||
- gomoddirectives
|
||||
- goprintffuncname
|
||||
- govet
|
||||
- importas
|
||||
- ineffassign
|
||||
- makezero
|
||||
- misspell
|
||||
- nakedret
|
||||
- nilerr
|
||||
- noctx
|
||||
- nolintlint
|
||||
- prealloc
|
||||
- predeclared
|
||||
- revive
|
||||
- rowserrcheck
|
||||
- sqlclosecheck
|
||||
- staticcheck
|
||||
- structcheck
|
||||
- stylecheck
|
||||
- tparallel
|
||||
- typecheck
|
||||
- unconvert
|
||||
- unparam
|
||||
- unused
|
||||
- varcheck
|
||||
- wastedassign
|
||||
- whitespace
|
||||
|
||||
# fixme
|
||||
# - cyclop
|
||||
# - errcheck
|
||||
# - errorlint
|
||||
# - exhaustivestruct
|
||||
# - forbidigo
|
||||
# - forcetypeassert
|
||||
# - gochecknoglobals
|
||||
# - gochecknoinits
|
||||
# - gocognit
|
||||
# - goconst
|
||||
# - gocritic
|
||||
# - gocyclo
|
||||
# - godot
|
||||
# - gosec
|
||||
# - gosimple
|
||||
# - ifshort
|
||||
# - lll
|
||||
# - nlreturn
|
||||
# - paralleltest
|
||||
# - scopelint
|
||||
# - thelper
|
||||
# - wrapcheck
|
||||
|
||||
# unused
|
||||
# - depguard
|
||||
# - goheader
|
||||
# - gomodguard
|
||||
|
||||
# don't enable:
|
||||
# - asciicheck
|
||||
# - funlen
|
||||
# - godox
|
||||
# - goerr113
|
||||
# - gomnd
|
||||
# - interfacer
|
||||
# - maligned
|
||||
# - nestif
|
||||
# - testpackage
|
||||
# - wsl
|
|
@ -1,24 +0,0 @@
|
|||
linters-settings:
|
||||
golint:
|
||||
min-confidence: 0.1
|
||||
goimports:
|
||||
local-prefixes: github.com/spf13/viper
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- funlen
|
||||
- maligned
|
||||
|
||||
# TODO: fix me
|
||||
- wsl
|
||||
- gochecknoinits
|
||||
- gosimple
|
||||
- gochecknoglobals
|
||||
- errcheck
|
||||
- lll
|
||||
- godox
|
||||
- scopelint
|
||||
- gocyclo
|
||||
- gocognit
|
||||
- gocritic
|
7
.idea/externalDependencies.xml
generated
7
.idea/externalDependencies.xml
generated
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalDependencies">
|
||||
<plugin id="name.kropp.intellij.makefile" />
|
||||
<plugin id="org.jetbrains.plugins.go" />
|
||||
</component>
|
||||
</project>
|
8
.idea/go.imports.xml
generated
8
.idea/go.imports.xml
generated
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GoImports">
|
||||
<option name="groupStdlibImports" value="true" />
|
||||
<option name="importSorting" value="GOFMT" />
|
||||
<option name="moveAllImportsInOneDeclaration" value="true" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/viper.iml" filepath="$PROJECT_DIR$/.idea/viper.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
7
.idea/runConfigurations/Check.xml
generated
7
.idea/runConfigurations/Check.xml
generated
|
@ -1,7 +0,0 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Check" type="CompoundRunConfigurationType">
|
||||
<toRun name="Tests" type="GoTestRunConfiguration" />
|
||||
<toRun name="Lint" type="MAKEFILE_TARGET_RUN_CONFIGURATION" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
8
.idea/runConfigurations/Lint.xml
generated
8
.idea/runConfigurations/Lint.xml
generated
|
@ -1,8 +0,0 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Lint" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
|
||||
<makefile filename="$PROJECT_DIR$/Makefile" target="lint" workingDirectory="" arguments="">
|
||||
<envs />
|
||||
</makefile>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
23
.idea/runConfigurations/Tests.xml
generated
23
.idea/runConfigurations/Tests.xml
generated
|
@ -1,23 +0,0 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Tests" type="GoTestRunConfiguration" factoryName="Go Test">
|
||||
<module name="viper" />
|
||||
<working_directory value="$PROJECT_DIR$/" />
|
||||
<go_parameters value="-i" />
|
||||
<EXTENSION ID="net.ashald.envfile">
|
||||
<option name="IS_ENABLED" value="false" />
|
||||
<option name="IS_SUBST" value="false" />
|
||||
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||
<ENTRIES>
|
||||
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||
</ENTRIES>
|
||||
</EXTENSION>
|
||||
<framework value="gotest" />
|
||||
<kind value="DIRECTORY" />
|
||||
<package value="github.com/spf13/viper" />
|
||||
<directory value="$PROJECT_DIR$/" />
|
||||
<filePath value="$PROJECT_DIR$/" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
9
.idea/viper.iml
generated
9
.idea/viper.iml
generated
|
@ -1,9 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
21
Makefile
21
Makefile
|
@ -1,9 +1,12 @@
|
|||
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
|
||||
|
||||
OS = $(shell uname | tr A-Z a-z)
|
||||
export PATH := $(abspath bin/):${PATH}
|
||||
|
||||
# Build variables
|
||||
BUILD_DIR ?= build
|
||||
export CGO_ENABLED ?= 0
|
||||
export GOOS = $(shell go env GOOS)
|
||||
ifeq (${VERBOSE}, 1)
|
||||
ifeq ($(filter -v,${GOARGS}),)
|
||||
GOARGS += -v
|
||||
|
@ -12,8 +15,8 @@ TEST_FORMAT = short-verbose
|
|||
endif
|
||||
|
||||
# Dependency versions
|
||||
GOTESTSUM_VERSION = 0.3.5
|
||||
GOLANGCI_VERSION = 1.21.0
|
||||
GOTESTSUM_VERSION = 1.8.0
|
||||
GOLANGCI_VERSION = 1.50.1
|
||||
|
||||
# Add the ability to override some variables
|
||||
# Use with care
|
||||
|
@ -33,21 +36,20 @@ bin/gotestsum-${GOTESTSUM_VERSION}:
|
|||
curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION}
|
||||
|
||||
TEST_PKGS ?= ./...
|
||||
TEST_REPORT_NAME ?= results.xml
|
||||
.PHONY: test
|
||||
test: TEST_REPORT ?= main
|
||||
test: TEST_FORMAT ?= short
|
||||
test: SHELL = /bin/bash
|
||||
test: export CGO_ENABLED=1
|
||||
test: bin/gotestsum ## Run tests
|
||||
@mkdir -p ${BUILD_DIR}/test_results/${TEST_REPORT}
|
||||
bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/test_results/${TEST_REPORT}/${TEST_REPORT_NAME} --format ${TEST_FORMAT} -- $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)
|
||||
@mkdir -p ${BUILD_DIR}
|
||||
bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/coverage.xml --format ${TEST_FORMAT} -- -race -coverprofile=${BUILD_DIR}/coverage.txt -covermode=atomic $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)
|
||||
|
||||
bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION}
|
||||
@ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint
|
||||
bin/golangci-lint-${GOLANGCI_VERSION}:
|
||||
@mkdir -p bin
|
||||
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}
|
||||
@mv bin/golangci-lint $@
|
||||
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION}
|
||||
@mv bin/golangci-lint "$@"
|
||||
|
||||
.PHONY: lint
|
||||
lint: bin/golangci-lint ## Run linter
|
||||
|
@ -57,6 +59,9 @@ lint: bin/golangci-lint ## Run linter
|
|||
fix: bin/golangci-lint ## Fix lint violations
|
||||
bin/golangci-lint run --fix
|
||||
|
||||
# Add custom targets here
|
||||
-include custom.mk
|
||||
|
||||
.PHONY: list
|
||||
list: ## List all make targets
|
||||
@${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
|
||||
|
|
249
README.md
249
README.md
|
@ -1,10 +1,20 @@
|
|||

|
||||
> ## Viper v2 feedback
|
||||
> Viper is heading towards v2 and we would love to hear what _**you**_ would like to see in it. Share your thoughts here: https://forms.gle/R6faU74qPRPAzchZ9
|
||||
>
|
||||
> **Thank you!**
|
||||
|
||||
Go configuration with fangs!
|
||||

|
||||
|
||||
[](https://github.com/spf13/viper)
|
||||
[](https://github.com/avelino/awesome-go#configuration)
|
||||
[](https://repl.it/@sagikazarmark/Viper-example#main.go)
|
||||
|
||||
[](https://github.com/spf13/viper/actions?query=workflow%3ACI)
|
||||
[](https://gitter.im/spf13/viper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://godoc.org/github.com/spf13/viper)
|
||||
[](https://goreportcard.com/report/github.com/spf13/viper)
|
||||

|
||||
[](https://pkg.go.dev/mod/github.com/spf13/viper)
|
||||
|
||||
**Go configuration with fangs!**
|
||||
|
||||
Many Go projects are built using Viper including:
|
||||
|
||||
|
@ -21,15 +31,17 @@ Many Go projects are built using Viper including:
|
|||
|
||||
## Install
|
||||
|
||||
```console
|
||||
```shell
|
||||
go get github.com/spf13/viper
|
||||
```
|
||||
|
||||
**Note:** Viper uses [Go Modules](https://github.com/golang/go/wiki/Modules) to manage dependencies.
|
||||
|
||||
|
||||
## What is Viper?
|
||||
|
||||
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed
|
||||
to work within an application, and can handle all types of configuration needs
|
||||
Viper is a complete configuration solution for Go applications including [12-Factor apps](https://12factor.net/#the_twelve_factors).
|
||||
It is designed to work within an application, and can handle all types of configuration needs
|
||||
and formats. It supports:
|
||||
|
||||
* setting defaults
|
||||
|
@ -101,12 +113,13 @@ where a configuration file is expected.
|
|||
|
||||
```go
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
||||
viper.AddConfigPath("/etc/appname/") // path to look for the config file in
|
||||
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
err := viper.ReadInConfig() // Find and read the config file
|
||||
if err != nil { // Handle errors reading the config file
|
||||
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
||||
panic(fmt.Errorf("fatal error config file: %w", err))
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -114,17 +127,17 @@ You can handle the specific case where no config file is found like this:
|
|||
|
||||
```go
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// Config file not found; ignore error if desired
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
}
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// Config file not found; ignore error if desired
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
}
|
||||
}
|
||||
|
||||
// Config file found and successfully parsed
|
||||
```
|
||||
|
||||
*NOTE:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc`
|
||||
*NOTE [since 1.6]:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc`
|
||||
|
||||
### Writing Config Files
|
||||
|
||||
|
@ -162,10 +175,10 @@ Optionally you can provide a function for Viper to run each time a change occurs
|
|||
**Make sure you add all of the configPaths prior to calling `WatchConfig()`**
|
||||
|
||||
```go
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("Config file changed:", e.Name)
|
||||
})
|
||||
viper.WatchConfig()
|
||||
```
|
||||
|
||||
### Reading Config from io.Reader
|
||||
|
@ -241,9 +254,10 @@ using `SetEnvPrefix`, you can tell Viper to use a prefix while reading from
|
|||
the environment variables. Both `BindEnv` and `AutomaticEnv` will use this
|
||||
prefix.
|
||||
|
||||
`BindEnv` takes one or two parameters. The first parameter is the key name, the
|
||||
second is the name of the environment variable. The name of the environment
|
||||
variable is case sensitive. If the ENV variable name is not provided, then
|
||||
`BindEnv` takes one or more parameters. The first parameter is the key name, the
|
||||
rest are the name of the environment variables to bind to this key. If more than
|
||||
one are provided, they will take precedence in the specified order. The name of
|
||||
the environment variable is case sensitive. If the ENV variable name is not provided, then
|
||||
Viper will automatically assume that the ENV variable matches the following format: prefix + "_" + the key name in ALL CAPS. When you explicitly provide the ENV variable name (the second parameter),
|
||||
it **does not** automatically add the prefix. For example if the second parameter is "id",
|
||||
Viper will look for the ENV variable "ID".
|
||||
|
@ -255,7 +269,7 @@ the `BindEnv` is called.
|
|||
`AutomaticEnv` is a powerful helper especially when combined with
|
||||
`SetEnvPrefix`. When called, Viper will check for an environment variable any
|
||||
time a `viper.Get` request is made. It will apply the following rules. It will
|
||||
check for a environment variable with a name matching the key uppercased and
|
||||
check for an environment variable with a name matching the key uppercased and
|
||||
prefixed with the `EnvPrefix` if set.
|
||||
|
||||
`SetEnvKeyReplacer` allows you to use a `strings.Replacer` object to rewrite Env
|
||||
|
@ -340,7 +354,7 @@ func main() {
|
|||
|
||||
i := viper.GetInt("flagname") // retrieve value from viper
|
||||
|
||||
...
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -399,7 +413,7 @@ in a Key/Value store such as etcd or Consul. These values take precedence over
|
|||
default values, but are overridden by configuration values retrieved from disk,
|
||||
flags, or environment variables.
|
||||
|
||||
Viper uses [crypt](https://github.com/xordataexchange/crypt) to retrieve
|
||||
Viper uses [crypt](https://github.com/bketelsen/crypt) to retrieve
|
||||
configuration from the K/V store, which means that you can store your
|
||||
configuration values encrypted and have them automatically decrypted if you have
|
||||
the correct gpg keyring. Encryption is optional.
|
||||
|
@ -411,7 +425,7 @@ independently of it.
|
|||
K/V store. `crypt` defaults to etcd on http://127.0.0.1:4001.
|
||||
|
||||
```bash
|
||||
$ go get github.com/xordataexchange/crypt/bin/crypt
|
||||
$ go get github.com/bketelsen/crypt/bin/crypt
|
||||
$ crypt set -plaintext /config/hugo.json /Users/hugo/settings/config.json
|
||||
```
|
||||
|
||||
|
@ -433,8 +447,15 @@ viper.SetConfigType("json") // because there is no file extension in a stream of
|
|||
err := viper.ReadRemoteConfig()
|
||||
```
|
||||
|
||||
#### etcd3
|
||||
```go
|
||||
viper.AddRemoteProvider("etcd3", "http://127.0.0.1:4001","/config/hugo.json")
|
||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
|
||||
err := viper.ReadRemoteConfig()
|
||||
```
|
||||
|
||||
#### Consul
|
||||
You need to set a key to Consul key/value storage with JSON value containing your desired config.
|
||||
You need to set a key to Consul key/value storage with JSON value containing your desired config.
|
||||
For example, create a Consul key/value store key `MY_CONSUL_KEY` with value:
|
||||
|
||||
```json
|
||||
|
@ -453,6 +474,16 @@ fmt.Println(viper.Get("port")) // 8080
|
|||
fmt.Println(viper.Get("hostname")) // myhostname.com
|
||||
```
|
||||
|
||||
#### Firestore
|
||||
|
||||
```go
|
||||
viper.AddRemoteProvider("firestore", "google-cloud-project-id", "collection/document")
|
||||
viper.SetConfigType("json") // Config's format: "json", "toml", "yaml", "yml"
|
||||
err := viper.ReadRemoteConfig()
|
||||
```
|
||||
|
||||
Of course, you're allowed to use `SecureRemoteProvider` also
|
||||
|
||||
### Remote Key/Value Store Example - Encrypted
|
||||
|
||||
```go
|
||||
|
@ -479,18 +510,18 @@ runtime_viper.Unmarshal(&runtime_conf)
|
|||
// open a goroutine to watch remote changes forever
|
||||
go func(){
|
||||
for {
|
||||
time.Sleep(time.Second * 5) // delay after each request
|
||||
time.Sleep(time.Second * 5) // delay after each request
|
||||
|
||||
// currently, only tested with etcd support
|
||||
err := runtime_viper.WatchRemoteConfig()
|
||||
if err != nil {
|
||||
log.Errorf("unable to read remote config: %v", err)
|
||||
continue
|
||||
}
|
||||
// currently, only tested with etcd support
|
||||
err := runtime_viper.WatchRemoteConfig()
|
||||
if err != nil {
|
||||
log.Errorf("unable to read remote config: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// unmarshal new config into our runtime config struct. you can also use channel
|
||||
// to implement a signal to notify the system of the changes
|
||||
runtime_viper.Unmarshal(&runtime_conf)
|
||||
// unmarshal new config into our runtime config struct. you can also use channel
|
||||
// to implement a signal to notify the system of the changes
|
||||
runtime_viper.Unmarshal(&runtime_conf)
|
||||
}
|
||||
}()
|
||||
```
|
||||
|
@ -522,7 +553,7 @@ Example:
|
|||
```go
|
||||
viper.GetString("logfile") // case-insensitive Setting & Getting
|
||||
if viper.GetBool("verbose") {
|
||||
fmt.Println("verbose enabled")
|
||||
fmt.Println("verbose enabled")
|
||||
}
|
||||
```
|
||||
### Accessing nested keys
|
||||
|
@ -568,10 +599,37 @@ the `Set()` method, …) with an immediate value, then all sub-keys of
|
|||
`datastore.metric` become undefined, they are “shadowed” by the higher-priority
|
||||
configuration level.
|
||||
|
||||
Viper can access array indices by using numbers in the path. For example:
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"host": {
|
||||
"address": "localhost",
|
||||
"ports": [
|
||||
5799,
|
||||
6029
|
||||
]
|
||||
},
|
||||
"datastore": {
|
||||
"metric": {
|
||||
"host": "127.0.0.1",
|
||||
"port": 3099
|
||||
},
|
||||
"warehouse": {
|
||||
"host": "198.0.0.1",
|
||||
"port": 2112
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GetInt("host.ports.1") // returns 6029
|
||||
|
||||
```
|
||||
|
||||
Lastly, if there exists a key that matches the delimited key path, its value
|
||||
will be returned instead. E.g.
|
||||
|
||||
```json
|
||||
```jsonc
|
||||
{
|
||||
"datastore.metric.host": "0.0.0.0",
|
||||
"host": {
|
||||
|
@ -593,14 +651,15 @@ will be returned instead. E.g.
|
|||
GetString("datastore.metric.host") // returns "0.0.0.0"
|
||||
```
|
||||
|
||||
### Extract sub-tree
|
||||
### Extracting a sub-tree
|
||||
|
||||
Extract sub-tree from Viper.
|
||||
When developing reusable modules, it's often useful to extract a subset of the configuration
|
||||
and pass it to a module. This way the module can be instantiated more than once, with different configurations.
|
||||
|
||||
For example, `viper` represents:
|
||||
For example, an application might use multiple different cache stores for different purposes:
|
||||
|
||||
```json
|
||||
app:
|
||||
```yaml
|
||||
cache:
|
||||
cache1:
|
||||
max-items: 100
|
||||
item-size: 64
|
||||
|
@ -609,35 +668,36 @@ app:
|
|||
item-size: 80
|
||||
```
|
||||
|
||||
After executing:
|
||||
We could pass the cache name to a module (eg. `NewCache("cache1")`),
|
||||
but it would require weird concatenation for accessing config keys and would be less separated from the global config.
|
||||
|
||||
So instead of doing that let's pass a Viper instance to the constructor that represents a subset of the configuration:
|
||||
|
||||
```go
|
||||
subv := viper.Sub("app.cache1")
|
||||
cache1Config := viper.Sub("cache.cache1")
|
||||
if cache1Config == nil { // Sub returns nil if the key cannot be found
|
||||
panic("cache configuration not found")
|
||||
}
|
||||
|
||||
cache1 := NewCache(cache1Config)
|
||||
```
|
||||
|
||||
`subv` represents:
|
||||
**Note:** Always check the return value of `Sub`. It returns `nil` if a key cannot be found.
|
||||
|
||||
```json
|
||||
max-items: 100
|
||||
item-size: 64
|
||||
```
|
||||
|
||||
Suppose we have:
|
||||
Internally, the `NewCache` function can address `max-items` and `item-size` keys directly:
|
||||
|
||||
```go
|
||||
func NewCache(cfg *Viper) *Cache {...}
|
||||
func NewCache(v *Viper) *Cache {
|
||||
return &Cache{
|
||||
MaxItems: v.GetInt("max-items"),
|
||||
ItemSize: v.GetInt("item-size"),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
which creates a cache based on config information formatted as `subv`.
|
||||
Now it’s easy to create these 2 caches separately as:
|
||||
The resulting code is easy to test, since it's decoupled from the main config structure,
|
||||
and easier to reuse (for the same reason).
|
||||
|
||||
```go
|
||||
cfg1 := viper.Sub("app.cache1")
|
||||
cache1 := NewCache(cfg1)
|
||||
|
||||
cfg2 := viper.Sub("app.cache2")
|
||||
cache2 := NewCache(cfg2)
|
||||
```
|
||||
|
||||
### Unmarshaling
|
||||
|
||||
|
@ -673,18 +733,18 @@ you have to change the delimiter:
|
|||
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
|
||||
|
||||
v.SetDefault("chart::values", map[string]interface{}{
|
||||
"ingress": map[string]interface{}{
|
||||
"annotations": map[string]interface{}{
|
||||
"traefik.frontend.rule.type": "PathPrefix",
|
||||
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
|
||||
},
|
||||
},
|
||||
"ingress": map[string]interface{}{
|
||||
"annotations": map[string]interface{}{
|
||||
"traefik.frontend.rule.type": "PathPrefix",
|
||||
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
type config struct {
|
||||
Chart struct{
|
||||
Values map[string]interface{}
|
||||
}
|
||||
Values map[string]interface{}
|
||||
}
|
||||
}
|
||||
|
||||
var C config
|
||||
|
@ -725,24 +785,33 @@ if err != nil {
|
|||
|
||||
Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default.
|
||||
|
||||
### Decoding custom formats
|
||||
|
||||
A frequently requested feature for Viper is adding more value formats and decoders.
|
||||
For example, parsing character (dot, comma, semicolon, etc) separated strings into slices.
|
||||
|
||||
This is already available in Viper using mapstructure decode hooks.
|
||||
|
||||
Read more about the details in [this blog post](https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/).
|
||||
|
||||
### Marshalling to string
|
||||
|
||||
You may need to marshal all the settings held in viper into a string rather than write them to a file.
|
||||
You may need to marshal all the settings held in viper into a string rather than write them to a file.
|
||||
You can use your favorite format's marshaller with the config returned by `AllSettings()`.
|
||||
|
||||
```go
|
||||
import (
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
// ...
|
||||
)
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
// ...
|
||||
)
|
||||
|
||||
func yamlStringSettings() string {
|
||||
c := viper.AllSettings()
|
||||
bs, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to marshal config to YAML: %v", err)
|
||||
}
|
||||
return string(bs)
|
||||
c := viper.AllSettings()
|
||||
bs, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to marshal config to YAML: %v", err)
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -778,15 +847,35 @@ y.SetDefault("ContentDir", "foobar")
|
|||
When working with multiple vipers, it is up to the user to keep track of the
|
||||
different vipers.
|
||||
|
||||
|
||||
## Q & A
|
||||
|
||||
Q: Why is it called “Viper”?
|
||||
### Why is it called “Viper”?
|
||||
|
||||
A: Viper is designed to be a [companion](http://en.wikipedia.org/wiki/Viper_(G.I._Joe))
|
||||
to [Cobra](https://github.com/spf13/cobra). While both can operate completely
|
||||
independently, together they make a powerful pair to handle much of your
|
||||
application foundation needs.
|
||||
|
||||
Q: Why is it called “Cobra”?
|
||||
### Why is it called “Cobra”?
|
||||
|
||||
A: Is there a better name for a [commander](http://en.wikipedia.org/wiki/Cobra_Commander)?
|
||||
Is there a better name for a [commander](http://en.wikipedia.org/wiki/Cobra_Commander)?
|
||||
|
||||
### Does Viper support case sensitive keys?
|
||||
|
||||
**tl;dr:** No.
|
||||
|
||||
Viper merges configuration from various sources, many of which are either case insensitive or uses different casing than the rest of the sources (eg. env vars).
|
||||
In order to provide the best experience when using multiple sources, the decision has been made to make all keys case insensitive.
|
||||
|
||||
There has been several attempts to implement case sensitivity, but unfortunately it's not that trivial. We might take a stab at implementing it in [Viper v2](https://github.com/spf13/viper/issues/772), but despite the initial noise, it does not seem to be requested that much.
|
||||
|
||||
You can vote for case sensitivity by filling out this feedback form: https://forms.gle/R6faU74qPRPAzchZ9
|
||||
|
||||
### Is it safe to concurrently read and write to a viper?
|
||||
|
||||
No, you will need to synchronize access to the viper yourself (for example by using the `sync` package). Concurrent reads and writes can cause a panic.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
See [TROUBLESHOOTING.md](TROUBLESHOOTING.md).
|
||||
|
|
32
TROUBLESHOOTING.md
Normal file
32
TROUBLESHOOTING.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Troubleshooting
|
||||
|
||||
## Unmarshaling doesn't work
|
||||
|
||||
The most common reason for this issue is improper use of struct tags (eg. `yaml` or `json`). Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default. Please refer to the library's documentation for using other struct tags.
|
||||
|
||||
## Cannot find package
|
||||
|
||||
Viper installation seems to fail a lot lately with the following (or a similar) error:
|
||||
|
||||
```
|
||||
cannot find package "github.com/hashicorp/hcl/tree/hcl1" in any of:
|
||||
/usr/local/Cellar/go/1.15.7_1/libexec/src/github.com/hashicorp/hcl/tree/hcl1 (from $GOROOT)
|
||||
/Users/user/go/src/github.com/hashicorp/hcl/tree/hcl1 (from $GOPATH)
|
||||
```
|
||||
|
||||
As the error message suggests, Go tries to look up dependencies in `GOPATH` mode (as it's commonly called) from the `GOPATH`.
|
||||
Viper opted to use [Go Modules](https://github.com/golang/go/wiki/Modules) to manage its dependencies. While in many cases the two methods are interchangeable, once a dependency releases new (major) versions, `GOPATH` mode is no longer able to decide which version to use, so it'll either use one that's already present or pick a version (usually the `master` branch).
|
||||
|
||||
The solution is easy: switch to using Go Modules.
|
||||
Please refer to the [wiki](https://github.com/golang/go/wiki/Modules) on how to do that.
|
||||
|
||||
**tl;dr* `export GO111MODULE=on`
|
||||
|
||||
## Unquoted 'y' and 'n' characters get replaced with _true_ and _false_ when reading a YAML file
|
||||
|
||||
This is a YAML 1.1 feature according to [go-yaml/yaml#740](https://github.com/go-yaml/yaml/issues/740).
|
||||
|
||||
Potential solutions are:
|
||||
|
||||
1. Quoting values resolved as boolean
|
||||
1. Upgrading to YAML v3 (for the time being this is possible by passing the `viper_yaml3` tag to your build)
|
11
experimental_logger.go
Normal file
11
experimental_logger.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
//go:build viper_logger
|
||||
// +build viper_logger
|
||||
|
||||
package viper
|
||||
|
||||
// WithLogger sets a custom logger.
|
||||
func WithLogger(l Logger) Option {
|
||||
return optionFunc(func(v *Viper) {
|
||||
v.logger = l
|
||||
})
|
||||
}
|
|
@ -10,13 +10,13 @@ import (
|
|||
func TestBindFlagValueSet(t *testing.T) {
|
||||
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||
|
||||
var testValues = map[string]*string{
|
||||
testValues := map[string]*string{
|
||||
"host": nil,
|
||||
"port": nil,
|
||||
"endpoint": nil,
|
||||
}
|
||||
|
||||
var mutatedTestValues = map[string]string{
|
||||
mutatedTestValues := map[string]string{
|
||||
"host": "localhost",
|
||||
"port": "6060",
|
||||
"endpoint": "/public",
|
||||
|
@ -44,8 +44,8 @@ func TestBindFlagValueSet(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestBindFlagValue(t *testing.T) {
|
||||
var testString = "testing"
|
||||
var testValue = newStringValue(testString, &testString)
|
||||
testString := "testing"
|
||||
testValue := newStringValue(testString, &testString)
|
||||
|
||||
flag := &pflag.Flag{
|
||||
Name: "testflag",
|
||||
|
@ -59,7 +59,7 @@ func TestBindFlagValue(t *testing.T) {
|
|||
assert.Equal(t, testString, Get("testvalue"))
|
||||
|
||||
flag.Value.Set("testing_mutate")
|
||||
flag.Changed = true //hack for pflag usage
|
||||
flag.Changed = true // hack for pflag usage
|
||||
|
||||
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
||||
}
|
||||
|
|
65
fs.go
Normal file
65
fs.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
//go:build go1.16 && finder
|
||||
// +build go1.16,finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"path"
|
||||
)
|
||||
|
||||
type finder struct {
|
||||
paths []string
|
||||
fileNames []string
|
||||
extensions []string
|
||||
|
||||
withoutExtension bool
|
||||
}
|
||||
|
||||
func (f finder) Find(fsys fs.FS) (string, error) {
|
||||
for _, searchPath := range f.paths {
|
||||
for _, fileName := range f.fileNames {
|
||||
for _, extension := range f.extensions {
|
||||
filePath := path.Join(searchPath, fileName+"."+extension)
|
||||
|
||||
ok, err := fileExists(fsys, filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ok {
|
||||
return filePath, nil
|
||||
}
|
||||
}
|
||||
|
||||
if f.withoutExtension {
|
||||
filePath := path.Join(searchPath, fileName)
|
||||
|
||||
ok, err := fileExists(fsys, filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ok {
|
||||
return filePath, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func fileExists(fsys fs.FS, filePath string) (bool, error) {
|
||||
fileInfo, err := fs.Stat(fsys, filePath)
|
||||
if err == nil {
|
||||
return !fileInfo.IsDir(), nil
|
||||
}
|
||||
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
100
fs_test.go
Normal file
100
fs_test.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
//go:build go1.16 && finder
|
||||
// +build go1.16,finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFinder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fsys := fstest.MapFS{
|
||||
"home/user/.config": &fstest.MapFile{},
|
||||
"home/user/config.json": &fstest.MapFile{},
|
||||
"home/user/config.yaml": &fstest.MapFile{},
|
||||
"home/user/data.json": &fstest.MapFile{},
|
||||
"etc/config/.config": &fstest.MapFile{},
|
||||
"etc/config/a_random_file.txt": &fstest.MapFile{},
|
||||
"etc/config/config.json": &fstest.MapFile{},
|
||||
"etc/config/config.yaml": &fstest.MapFile{},
|
||||
"etc/config/config.xml": &fstest.MapFile{},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
fsys func() fs.FS
|
||||
finder finder
|
||||
result string
|
||||
}{
|
||||
{
|
||||
name: "find file",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"etc/config"},
|
||||
fileNames: []string{"config"},
|
||||
extensions: []string{"json"},
|
||||
},
|
||||
result: "etc/config/config.json",
|
||||
},
|
||||
{
|
||||
name: "file not found",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config"},
|
||||
fileNames: []string{"config"},
|
||||
extensions: []string{"json"},
|
||||
},
|
||||
result: "",
|
||||
},
|
||||
{
|
||||
name: "empty search params",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{},
|
||||
result: "",
|
||||
},
|
||||
{
|
||||
name: "precedence",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config", "home/user", "etc/config"},
|
||||
fileNames: []string{"aconfig", "config"},
|
||||
extensions: []string{"zml", "xml", "json"},
|
||||
},
|
||||
result: "home/user/config.json",
|
||||
},
|
||||
{
|
||||
name: "without extension",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config", "home/user", "etc/config"},
|
||||
fileNames: []string{".config"},
|
||||
extensions: []string{"zml", "xml", "json"},
|
||||
|
||||
withoutExtension: true,
|
||||
},
|
||||
result: "home/user/.config",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
testCase := testCase
|
||||
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fsys := testCase.fsys()
|
||||
|
||||
result, err := testCase.finder.Find(fsys)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, testCase.result, result)
|
||||
})
|
||||
}
|
||||
}
|
109
go.mod
109
go.mod
|
@ -1,48 +1,75 @@
|
|||
module github.com/ShaleApps/viper
|
||||
module github.com/spf13/viper
|
||||
|
||||
go 1.12
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
|
||||
github.com/coreos/bbolt v1.3.2 // indirect
|
||||
github.com/coreos/etcd v3.3.10+incompatible // indirect
|
||||
github.com/coreos/go-semver v0.2.0 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect
|
||||
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
github.com/gogo/protobuf v1.2.1 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
|
||||
github.com/google/btree v1.0.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.0 // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.9.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0
|
||||
github.com/hashicorp/hcl v1.0.0
|
||||
github.com/jonboulle/clockwork v0.1.0 // indirect
|
||||
github.com/magiconair/properties v1.8.1
|
||||
github.com/mitchellh/mapstructure v1.1.2
|
||||
github.com/pelletier/go-toml v1.6.0
|
||||
github.com/prometheus/client_golang v0.9.3 // indirect
|
||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||
github.com/soheilhy/cmux v0.1.4 // indirect
|
||||
github.com/spf13/afero v1.2.2
|
||||
github.com/spf13/cast v1.3.1
|
||||
github.com/magiconair/properties v1.8.7
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/pelletier/go-toml/v2 v2.0.6
|
||||
github.com/sagikazarmark/crypt v0.9.0
|
||||
github.com/spf13/afero v1.9.3
|
||||
github.com/spf13/cast v1.5.0
|
||||
github.com/spf13/jwalterweatherman v1.1.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/subosito/gotenv v1.2.0
|
||||
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
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77
|
||||
go.etcd.io/bbolt v1.3.2 // indirect
|
||||
go.uber.org/atomic v1.4.0 // indirect
|
||||
go.uber.org/multierr v1.1.0 // indirect
|
||||
go.uber.org/zap v1.10.0 // indirect
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
||||
google.golang.org/grpc v1.21.0 // indirect
|
||||
gopkg.in/ini.v1 v1.51.1
|
||||
gopkg.in/yaml.v2 v2.2.7
|
||||
github.com/stretchr/testify v1.8.1
|
||||
github.com/subosito/gotenv v1.4.2
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.105.0 // indirect
|
||||
cloud.google.com/go/compute v1.14.0 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.2.3 // indirect
|
||||
cloud.google.com/go/firestore v1.9.0 // indirect
|
||||
cloud.google.com/go/longrunning v0.3.0 // indirect
|
||||
github.com/armon/go-metrics v0.4.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
github.com/googleapis/enterprise-certificate-proxy v0.2.1 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
|
||||
github.com/hashicorp/consul/api v1.18.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/hashicorp/go-hclog v1.2.0 // indirect
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/hashicorp/serf v0.10.1 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.6 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.6 // indirect
|
||||
go.etcd.io/etcd/client/v2 v2.305.6 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.6 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
|
||||
golang.org/x/net v0.4.0 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.3.0 // indirect
|
||||
golang.org/x/text v0.5.0 // indirect
|
||||
golang.org/x/time v0.1.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
|
||||
google.golang.org/api v0.107.0 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
|
||||
google.golang.org/grpc v1.52.0 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
)
|
||||
|
|
61
internal/encoding/decoder.go
Normal file
61
internal/encoding/decoder.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Decoder decodes the contents of b into v.
|
||||
// It's primarily used for decoding contents of a file into a map[string]interface{}.
|
||||
type Decoder interface {
|
||||
Decode(b []byte, v map[string]interface{}) error
|
||||
}
|
||||
|
||||
const (
|
||||
// ErrDecoderNotFound is returned when there is no decoder registered for a format.
|
||||
ErrDecoderNotFound = encodingError("decoder not found for this format")
|
||||
|
||||
// ErrDecoderFormatAlreadyRegistered is returned when an decoder is already registered for a format.
|
||||
ErrDecoderFormatAlreadyRegistered = encodingError("decoder already registered for this format")
|
||||
)
|
||||
|
||||
// DecoderRegistry can choose an appropriate Decoder based on the provided format.
|
||||
type DecoderRegistry struct {
|
||||
decoders map[string]Decoder
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewDecoderRegistry returns a new, initialized DecoderRegistry.
|
||||
func NewDecoderRegistry() *DecoderRegistry {
|
||||
return &DecoderRegistry{
|
||||
decoders: make(map[string]Decoder),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterDecoder registers a Decoder for a format.
|
||||
// Registering a Decoder for an already existing format is not supported.
|
||||
func (e *DecoderRegistry) RegisterDecoder(format string, enc Decoder) error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if _, ok := e.decoders[format]; ok {
|
||||
return ErrDecoderFormatAlreadyRegistered
|
||||
}
|
||||
|
||||
e.decoders[format] = enc
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode calls the underlying Decoder based on the format.
|
||||
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]interface{}) error {
|
||||
e.mu.RLock()
|
||||
decoder, ok := e.decoders[format]
|
||||
e.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return ErrDecoderNotFound
|
||||
}
|
||||
|
||||
return decoder.Decode(b, v)
|
||||
}
|
81
internal/encoding/decoder_test.go
Normal file
81
internal/encoding/decoder_test.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type decoder struct {
|
||||
v map[string]interface{}
|
||||
}
|
||||
|
||||
func (d decoder) Decode(_ []byte, v map[string]interface{}) error {
|
||||
for key, value := range d.v {
|
||||
v[key] = value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestDecoderRegistry_RegisterDecoder(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("AlreadyRegistered", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = registry.RegisterDecoder("myformat", decoder{})
|
||||
if err != ErrDecoderFormatAlreadyRegistered {
|
||||
t.Fatalf("expected ErrDecoderFormatAlreadyRegistered, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecoderRegistry_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
decoder := decoder{
|
||||
v: map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err = registry.Decode("myformat", []byte("key: value"), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(decoder.v, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %+v\nexpected: %+v", v, decoder.v)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecoderNotFound", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := registry.Decode("myformat", nil, v)
|
||||
if err != ErrDecoderNotFound {
|
||||
t.Fatalf("expected ErrDecoderNotFound, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
61
internal/encoding/dotenv/codec.go
Normal file
61
internal/encoding/dotenv/codec.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package dotenv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/subosito/gotenv"
|
||||
)
|
||||
|
||||
const keyDelimiter = "_"
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for encoding data containing environment variables
|
||||
// (commonly called as dotenv format).
|
||||
type Codec struct{}
|
||||
|
||||
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
flattened := map[string]interface{}{}
|
||||
|
||||
flattened = flattenAndMergeMap(flattened, v, "", keyDelimiter)
|
||||
|
||||
keys := make([]string, 0, len(flattened))
|
||||
|
||||
for key := range flattened {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
for _, key := range keys {
|
||||
_, err := buf.WriteString(fmt.Sprintf("%v=%v\n", strings.ToUpper(key), flattened[key]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
var buf bytes.Buffer
|
||||
|
||||
_, err := buf.Write(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
env, err := gotenv.StrictParse(&buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for key, value := range env {
|
||||
v[key] = value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
63
internal/encoding/dotenv/codec_test.go
Normal file
63
internal/encoding/dotenv/codec_test.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package dotenv
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `# key-value pair
|
||||
KEY=value
|
||||
`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `KEY=value
|
||||
`
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"KEY": "value",
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
41
internal/encoding/dotenv/map_utils.go
Normal file
41
internal/encoding/dotenv/map_utils.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package dotenv
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||
// Code is based on the function with the same name in tha main package.
|
||||
// TODO: move it to a common place
|
||||
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
|
||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||
// prefix is shadowed => nothing more to flatten
|
||||
return shadow
|
||||
}
|
||||
if shadow == nil {
|
||||
shadow = make(map[string]interface{})
|
||||
}
|
||||
|
||||
var m2 map[string]interface{}
|
||||
if prefix != "" {
|
||||
prefix += delimiter
|
||||
}
|
||||
for k, val := range m {
|
||||
fullKey := prefix + k
|
||||
switch val.(type) {
|
||||
case map[string]interface{}:
|
||||
m2 = val.(map[string]interface{})
|
||||
case map[interface{}]interface{}:
|
||||
m2 = cast.ToStringMap(val)
|
||||
default:
|
||||
// immediate value
|
||||
shadow[strings.ToLower(fullKey)] = val
|
||||
continue
|
||||
}
|
||||
// recursively merge to shadow map
|
||||
shadow = flattenAndMergeMap(shadow, m2, fullKey, delimiter)
|
||||
}
|
||||
return shadow
|
||||
}
|
60
internal/encoding/encoder.go
Normal file
60
internal/encoding/encoder.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Encoder encodes the contents of v into a byte representation.
|
||||
// It's primarily used for encoding a map[string]interface{} into a file format.
|
||||
type Encoder interface {
|
||||
Encode(v map[string]interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
const (
|
||||
// ErrEncoderNotFound is returned when there is no encoder registered for a format.
|
||||
ErrEncoderNotFound = encodingError("encoder not found for this format")
|
||||
|
||||
// ErrEncoderFormatAlreadyRegistered is returned when an encoder is already registered for a format.
|
||||
ErrEncoderFormatAlreadyRegistered = encodingError("encoder already registered for this format")
|
||||
)
|
||||
|
||||
// EncoderRegistry can choose an appropriate Encoder based on the provided format.
|
||||
type EncoderRegistry struct {
|
||||
encoders map[string]Encoder
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewEncoderRegistry returns a new, initialized EncoderRegistry.
|
||||
func NewEncoderRegistry() *EncoderRegistry {
|
||||
return &EncoderRegistry{
|
||||
encoders: make(map[string]Encoder),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterEncoder registers an Encoder for a format.
|
||||
// Registering a Encoder for an already existing format is not supported.
|
||||
func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if _, ok := e.encoders[format]; ok {
|
||||
return ErrEncoderFormatAlreadyRegistered
|
||||
}
|
||||
|
||||
e.encoders[format] = enc
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EncoderRegistry) Encode(format string, v map[string]interface{}) ([]byte, error) {
|
||||
e.mu.RLock()
|
||||
encoder, ok := e.encoders[format]
|
||||
e.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return nil, ErrEncoderNotFound
|
||||
}
|
||||
|
||||
return encoder.Encode(v)
|
||||
}
|
70
internal/encoding/encoder_test.go
Normal file
70
internal/encoding/encoder_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type encoder struct {
|
||||
b []byte
|
||||
}
|
||||
|
||||
func (e encoder) Encode(_ map[string]interface{}) ([]byte, error) {
|
||||
return e.b, nil
|
||||
}
|
||||
|
||||
func TestEncoderRegistry_RegisterEncoder(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("AlreadyRegistered", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = registry.RegisterEncoder("myformat", encoder{})
|
||||
if err != ErrEncoderFormatAlreadyRegistered {
|
||||
t.Fatalf("expected ErrEncoderFormatAlreadyRegistered, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncoderRegistry_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
encoder := encoder{
|
||||
b: []byte("key: value"),
|
||||
}
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := registry.Encode("myformat", map[string]interface{}{"key": "value"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(b) != "key: value" {
|
||||
t.Fatalf("expected 'key: value', got: %#v", string(b))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("EncoderNotFound", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
_, err := registry.Encode("myformat", map[string]interface{}{"key": "value"})
|
||||
if err != ErrEncoderNotFound {
|
||||
t.Fatalf("expected ErrEncoderNotFound, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
7
internal/encoding/error.go
Normal file
7
internal/encoding/error.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package encoding
|
||||
|
||||
type encodingError string
|
||||
|
||||
func (e encodingError) Error() string {
|
||||
return string(e)
|
||||
}
|
40
internal/encoding/hcl/codec.go
Normal file
40
internal/encoding/hcl/codec.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package hcl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/hashicorp/hcl"
|
||||
"github.com/hashicorp/hcl/hcl/printer"
|
||||
)
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for HCL encoding.
|
||||
// TODO: add printer config to the codec?
|
||||
type Codec struct{}
|
||||
|
||||
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: use printer.Format? Is the trailing newline an issue?
|
||||
|
||||
ast, err := hcl.Parse(string(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
err = printer.Fprint(&buf, ast.Node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
return hcl.Unmarshal(b, &v)
|
||||
}
|
140
internal/encoding/hcl/codec_test.go
Normal file
140
internal/encoding/hcl/codec_test.go
Normal file
|
@ -0,0 +1,140 @@
|
|||
package hcl
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `# key-value pair
|
||||
"key" = "value"
|
||||
|
||||
// list
|
||||
"list" = ["item1", "item2", "item3"]
|
||||
|
||||
/* map */
|
||||
"map" = {
|
||||
"key" = "value"
|
||||
}
|
||||
|
||||
/*
|
||||
nested map
|
||||
*/
|
||||
"nested_map" "map" {
|
||||
"key" = "value"
|
||||
|
||||
"list" = ["item1", "item2", "item3"]
|
||||
}`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `"key" = "value"
|
||||
|
||||
"list" = ["item1", "item2", "item3"]
|
||||
|
||||
"map" = {
|
||||
"key" = "value"
|
||||
}
|
||||
|
||||
"nested_map" "map" {
|
||||
"key" = "value"
|
||||
|
||||
"list" = ["item1", "item2", "item3"]
|
||||
}`
|
||||
|
||||
// decoded form of the data
|
||||
//
|
||||
// in case of HCL it's slightly different from Viper's internal representation
|
||||
// (eg. map is decoded into a list of maps)
|
||||
var decoded = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": []map[string]interface{}{
|
||||
{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
"nested_map": []map[string]interface{}{
|
||||
{
|
||||
"map": []map[string]interface{}{
|
||||
{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"nested_map": map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(decoded, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, decoded)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
99
internal/encoding/ini/codec.go
Normal file
99
internal/encoding/ini/codec.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// LoadOptions contains all customized options used for load data source(s).
|
||||
// This type is added here for convenience: this way consumers can import a single package called "ini".
|
||||
type LoadOptions = ini.LoadOptions
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for INI encoding.
|
||||
type Codec struct {
|
||||
KeyDelimiter string
|
||||
LoadOptions LoadOptions
|
||||
}
|
||||
|
||||
func (c Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
cfg := ini.Empty()
|
||||
ini.PrettyFormat = false
|
||||
|
||||
flattened := map[string]interface{}{}
|
||||
|
||||
flattened = flattenAndMergeMap(flattened, v, "", c.keyDelimiter())
|
||||
|
||||
keys := make([]string, 0, len(flattened))
|
||||
|
||||
for key := range flattened {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
sectionName, keyName := "", key
|
||||
|
||||
lastSep := strings.LastIndex(key, ".")
|
||||
if lastSep != -1 {
|
||||
sectionName = key[:(lastSep)]
|
||||
keyName = key[(lastSep + 1):]
|
||||
}
|
||||
|
||||
// TODO: is this a good idea?
|
||||
if sectionName == "default" {
|
||||
sectionName = ""
|
||||
}
|
||||
|
||||
cfg.Section(sectionName).Key(keyName).SetValue(cast.ToString(flattened[key]))
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
_, err := cfg.WriteTo(&buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
cfg := ini.Empty(c.LoadOptions)
|
||||
|
||||
err := cfg.Append(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sections := cfg.Sections()
|
||||
|
||||
for i := 0; i < len(sections); i++ {
|
||||
section := sections[i]
|
||||
keys := section.Keys()
|
||||
|
||||
for j := 0; j < len(keys); j++ {
|
||||
key := keys[j]
|
||||
value := cfg.Section(section.Name()).Key(key.Name()).String()
|
||||
|
||||
deepestMap := deepSearch(v, strings.Split(section.Name(), c.keyDelimiter()))
|
||||
|
||||
// set innermost value
|
||||
deepestMap[key.Name()] = value
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Codec) keyDelimiter() string {
|
||||
if c.KeyDelimiter == "" {
|
||||
return "."
|
||||
}
|
||||
|
||||
return c.KeyDelimiter
|
||||
}
|
111
internal/encoding/ini/codec_test.go
Normal file
111
internal/encoding/ini/codec_test.go
Normal file
|
@ -0,0 +1,111 @@
|
|||
package ini
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `; key-value pair
|
||||
key=value ; key-value pair
|
||||
|
||||
# map
|
||||
[map] # map
|
||||
key=%(key)s
|
||||
|
||||
`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `key=value
|
||||
|
||||
[map]
|
||||
key=value
|
||||
`
|
||||
|
||||
// decoded form of the data
|
||||
//
|
||||
// in case of INI it's slightly different from Viper's internal representation
|
||||
// (eg. top level keys land in a section called default)
|
||||
var decoded = map[string]interface{}{
|
||||
"DEFAULT": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Default", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"default": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(decoded, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, decoded)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
74
internal/encoding/ini/map_utils.go
Normal file
74
internal/encoding/ini/map_utils.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package ini
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// THIS CODE IS COPIED HERE: IT SHOULD NOT BE MODIFIED
|
||||
// AT SOME POINT IT WILL BE MOVED TO A COMMON PLACE
|
||||
// deepSearch scans deep maps, following the key indexes listed in the
|
||||
// sequence "path".
|
||||
// The last value is expected to be another map, and is returned.
|
||||
//
|
||||
// In case intermediate keys do not exist, or map to a non-map value,
|
||||
// a new map is created and inserted, and the search continues from there:
|
||||
// the initial map "m" may be modified!
|
||||
func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
|
||||
for _, k := range path {
|
||||
m2, ok := m[k]
|
||||
if !ok {
|
||||
// intermediate key does not exist
|
||||
// => create it and continue from there
|
||||
m3 := make(map[string]interface{})
|
||||
m[k] = m3
|
||||
m = m3
|
||||
continue
|
||||
}
|
||||
m3, ok := m2.(map[string]interface{})
|
||||
if !ok {
|
||||
// intermediate key is a value
|
||||
// => replace with a new map
|
||||
m3 = make(map[string]interface{})
|
||||
m[k] = m3
|
||||
}
|
||||
// continue search from here
|
||||
m = m3
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||
// Code is based on the function with the same name in tha main package.
|
||||
// TODO: move it to a common place
|
||||
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
|
||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||
// prefix is shadowed => nothing more to flatten
|
||||
return shadow
|
||||
}
|
||||
if shadow == nil {
|
||||
shadow = make(map[string]interface{})
|
||||
}
|
||||
|
||||
var m2 map[string]interface{}
|
||||
if prefix != "" {
|
||||
prefix += delimiter
|
||||
}
|
||||
for k, val := range m {
|
||||
fullKey := prefix + k
|
||||
switch val.(type) {
|
||||
case map[string]interface{}:
|
||||
m2 = val.(map[string]interface{})
|
||||
case map[interface{}]interface{}:
|
||||
m2 = cast.ToStringMap(val)
|
||||
default:
|
||||
// immediate value
|
||||
shadow[strings.ToLower(fullKey)] = val
|
||||
continue
|
||||
}
|
||||
// recursively merge to shadow map
|
||||
shadow = flattenAndMergeMap(shadow, m2, fullKey, delimiter)
|
||||
}
|
||||
return shadow
|
||||
}
|
86
internal/encoding/javaproperties/codec.go
Normal file
86
internal/encoding/javaproperties/codec.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package javaproperties
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/magiconair/properties"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for Java properties encoding.
|
||||
type Codec struct {
|
||||
KeyDelimiter string
|
||||
|
||||
// Store read properties on the object so that we can write back in order with comments.
|
||||
// This will only be used if the configuration read is a properties file.
|
||||
// TODO: drop this feature in v2
|
||||
// TODO: make use of the global properties object optional
|
||||
Properties *properties.Properties
|
||||
}
|
||||
|
||||
func (c *Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
if c.Properties == nil {
|
||||
c.Properties = properties.NewProperties()
|
||||
}
|
||||
|
||||
flattened := map[string]interface{}{}
|
||||
|
||||
flattened = flattenAndMergeMap(flattened, v, "", c.keyDelimiter())
|
||||
|
||||
keys := make([]string, 0, len(flattened))
|
||||
|
||||
for key := range flattened {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
_, _, err := c.Properties.Set(key, cast.ToString(flattened[key]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
_, err := c.Properties.WriteComment(&buf, "#", properties.UTF8)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
var err error
|
||||
c.Properties, err = properties.Load(b, properties.UTF8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, key := range c.Properties.Keys() {
|
||||
// ignore existence check: we know it's there
|
||||
value, _ := c.Properties.Get(key)
|
||||
|
||||
// recursively build nested maps
|
||||
path := strings.Split(key, c.keyDelimiter())
|
||||
lastKey := strings.ToLower(path[len(path)-1])
|
||||
deepestMap := deepSearch(v, path[0:len(path)-1])
|
||||
|
||||
// set innermost value
|
||||
deepestMap[lastKey] = value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Codec) keyDelimiter() string {
|
||||
if c.KeyDelimiter == "" {
|
||||
return "."
|
||||
}
|
||||
|
||||
return c.KeyDelimiter
|
||||
}
|
89
internal/encoding/javaproperties/codec_test.go
Normal file
89
internal/encoding/javaproperties/codec_test.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
package javaproperties
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `#key-value pair
|
||||
key = value
|
||||
map.key = value
|
||||
`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `key = value
|
||||
map.key = value
|
||||
`
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
t.Skip("TODO: needs invalid data example")
|
||||
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
codec.Decode([]byte(``), v)
|
||||
|
||||
if len(v) > 0 {
|
||||
t.Fatalf("expected map to be empty when data is invalid\nactual: %#v", v)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCodec_DecodeEncode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if original != string(b) {
|
||||
t.Fatalf("encoded value does not match the original\nactual: %#v\nexpected: %#v", string(b), original)
|
||||
}
|
||||
}
|
74
internal/encoding/javaproperties/map_utils.go
Normal file
74
internal/encoding/javaproperties/map_utils.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package javaproperties
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// THIS CODE IS COPIED HERE: IT SHOULD NOT BE MODIFIED
|
||||
// AT SOME POINT IT WILL BE MOVED TO A COMMON PLACE
|
||||
// deepSearch scans deep maps, following the key indexes listed in the
|
||||
// sequence "path".
|
||||
// The last value is expected to be another map, and is returned.
|
||||
//
|
||||
// In case intermediate keys do not exist, or map to a non-map value,
|
||||
// a new map is created and inserted, and the search continues from there:
|
||||
// the initial map "m" may be modified!
|
||||
func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
|
||||
for _, k := range path {
|
||||
m2, ok := m[k]
|
||||
if !ok {
|
||||
// intermediate key does not exist
|
||||
// => create it and continue from there
|
||||
m3 := make(map[string]interface{})
|
||||
m[k] = m3
|
||||
m = m3
|
||||
continue
|
||||
}
|
||||
m3, ok := m2.(map[string]interface{})
|
||||
if !ok {
|
||||
// intermediate key is a value
|
||||
// => replace with a new map
|
||||
m3 = make(map[string]interface{})
|
||||
m[k] = m3
|
||||
}
|
||||
// continue search from here
|
||||
m = m3
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||
// Code is based on the function with the same name in tha main package.
|
||||
// TODO: move it to a common place
|
||||
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
|
||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||
// prefix is shadowed => nothing more to flatten
|
||||
return shadow
|
||||
}
|
||||
if shadow == nil {
|
||||
shadow = make(map[string]interface{})
|
||||
}
|
||||
|
||||
var m2 map[string]interface{}
|
||||
if prefix != "" {
|
||||
prefix += delimiter
|
||||
}
|
||||
for k, val := range m {
|
||||
fullKey := prefix + k
|
||||
switch val.(type) {
|
||||
case map[string]interface{}:
|
||||
m2 = val.(map[string]interface{})
|
||||
case map[interface{}]interface{}:
|
||||
m2 = cast.ToStringMap(val)
|
||||
default:
|
||||
// immediate value
|
||||
shadow[strings.ToLower(fullKey)] = val
|
||||
continue
|
||||
}
|
||||
// recursively merge to shadow map
|
||||
shadow = flattenAndMergeMap(shadow, m2, fullKey, delimiter)
|
||||
}
|
||||
return shadow
|
||||
}
|
17
internal/encoding/json/codec.go
Normal file
17
internal/encoding/json/codec.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for JSON encoding.
|
||||
type Codec struct{}
|
||||
|
||||
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
// TODO: expose prefix and indent in the Codec as setting?
|
||||
return json.MarshalIndent(v, "", " ")
|
||||
}
|
||||
|
||||
func (Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
return json.Unmarshal(b, &v)
|
||||
}
|
95
internal/encoding/json/codec_test.go
Normal file
95
internal/encoding/json/codec_test.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `{
|
||||
"key": "value",
|
||||
"list": [
|
||||
"item1",
|
||||
"item2",
|
||||
"item3"
|
||||
],
|
||||
"map": {
|
||||
"key": "value"
|
||||
},
|
||||
"nested_map": {
|
||||
"map": {
|
||||
"key": "value",
|
||||
"list": [
|
||||
"item1",
|
||||
"item2",
|
||||
"item3"
|
||||
]
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"nested_map": map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(encoded), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
16
internal/encoding/toml/codec.go
Normal file
16
internal/encoding/toml/codec.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package toml
|
||||
|
||||
import (
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding.
|
||||
type Codec struct{}
|
||||
|
||||
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
return toml.Marshal(v)
|
||||
}
|
||||
|
||||
func (Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
return toml.Unmarshal(b, &v)
|
||||
}
|
105
internal/encoding/toml/codec_test.go
Normal file
105
internal/encoding/toml/codec_test.go
Normal file
|
@ -0,0 +1,105 @@
|
|||
package toml
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `# key-value pair
|
||||
key = "value"
|
||||
list = ["item1", "item2", "item3"]
|
||||
|
||||
[map]
|
||||
key = "value"
|
||||
|
||||
# nested
|
||||
# map
|
||||
[nested_map]
|
||||
[nested_map.map]
|
||||
key = "value"
|
||||
list = [
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
]
|
||||
`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `key = 'value'
|
||||
list = ['item1', 'item2', 'item3']
|
||||
|
||||
[map]
|
||||
key = 'value'
|
||||
|
||||
[nested_map]
|
||||
[nested_map.map]
|
||||
key = 'value'
|
||||
list = ['item1', 'item2', 'item3']
|
||||
`
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"nested_map": map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
14
internal/encoding/yaml/codec.go
Normal file
14
internal/encoding/yaml/codec.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package yaml
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for YAML encoding.
|
||||
type Codec struct{}
|
||||
|
||||
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
|
||||
return yaml.Marshal(v)
|
||||
}
|
||||
|
||||
func (Codec) Decode(b []byte, v map[string]interface{}) error {
|
||||
return yaml.Unmarshal(b, &v)
|
||||
}
|
136
internal/encoding/yaml/codec_test.go
Normal file
136
internal/encoding/yaml/codec_test.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
package yaml
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// original form of the data
|
||||
const original = `# key-value pair
|
||||
key: value
|
||||
list:
|
||||
- item1
|
||||
- item2
|
||||
- item3
|
||||
map:
|
||||
key: value
|
||||
|
||||
# nested
|
||||
# map
|
||||
nested_map:
|
||||
map:
|
||||
key: value
|
||||
list:
|
||||
- item1
|
||||
- item2
|
||||
- item3
|
||||
`
|
||||
|
||||
// encoded form of the data
|
||||
const encoded = `key: value
|
||||
list:
|
||||
- item1
|
||||
- item2
|
||||
- item3
|
||||
map:
|
||||
key: value
|
||||
nested_map:
|
||||
map:
|
||||
key: value
|
||||
list:
|
||||
- item1
|
||||
- item2
|
||||
- item3
|
||||
`
|
||||
|
||||
// decoded form of the data
|
||||
//
|
||||
// in case of YAML it's slightly different from Viper's internal representation
|
||||
// (eg. map is decoded into a map with interface key)
|
||||
var decoded = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"nested_map": map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Viper's internal representation
|
||||
var data = map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
"nested_map": map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"key": "value",
|
||||
"list": []interface{}{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCodec_Encode(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
b, err := codec.Encode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if encoded != string(b) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(original), v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(decoded, v) {
|
||||
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, decoded)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidData", func(t *testing.T) {
|
||||
codec := Codec{}
|
||||
|
||||
v := map[string]interface{}{}
|
||||
|
||||
err := codec.Decode([]byte(`invalid data`), v)
|
||||
if err == nil {
|
||||
t.Fatal("expected decoding to fail")
|
||||
}
|
||||
|
||||
t.Logf("decoding failed as expected: %s", err)
|
||||
})
|
||||
}
|
40
internal/testutil/env_go1_16.go
Normal file
40
internal/testutil/env_go1_16.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
//go:build !go1.17
|
||||
// +build !go1.17
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Based on https://github.com/frankban/quicktest/blob/577841610793d24f99e31cc2c0ef3a541fefd7c7/patch.go#L34-L64
|
||||
// Licensed under the MIT license
|
||||
// Copyright (c) 2017 Canonical Ltd.
|
||||
|
||||
// Setenv sets an environment variable to a temporary value for the
|
||||
// duration of the test.
|
||||
//
|
||||
// At the end of the test (see "Deferred execution" in the package docs), the
|
||||
// environment variable is returned to its original value.
|
||||
func Setenv(t *testing.T, name, val string) {
|
||||
setenv(t, name, val, true)
|
||||
}
|
||||
|
||||
// setenv sets or unsets an environment variable to a temporary value for the
|
||||
// duration of the test
|
||||
func setenv(t *testing.T, name, val string, valOK bool) {
|
||||
oldVal, oldOK := os.LookupEnv(name)
|
||||
if valOK {
|
||||
os.Setenv(name, val)
|
||||
} else {
|
||||
os.Unsetenv(name)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if oldOK {
|
||||
os.Setenv(name, oldVal)
|
||||
} else {
|
||||
os.Unsetenv(name)
|
||||
}
|
||||
})
|
||||
}
|
18
internal/testutil/env_go1_17.go
Normal file
18
internal/testutil/env_go1_17.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
//go:build go1.17
|
||||
// +build go1.17
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Setenv sets an environment variable to a temporary value for the
|
||||
// duration of the test.
|
||||
//
|
||||
// This shim can be removed once support for Go <1.17 is dropped.
|
||||
func Setenv(t *testing.T, name, val string) {
|
||||
t.Helper()
|
||||
|
||||
t.Setenv(name, val)
|
||||
}
|
18
internal/testutil/filepath.go
Normal file
18
internal/testutil/filepath.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// AbsFilePath calls filepath.Abs on path.
|
||||
func AbsFilePath(t *testing.T, path string) string {
|
||||
t.Helper()
|
||||
|
||||
s, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
77
logger.go
Normal file
77
logger.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package viper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
// Logger is a unified interface for various logging use cases and practices, including:
|
||||
// - leveled logging
|
||||
// - structured logging
|
||||
type Logger interface {
|
||||
// Trace logs a Trace event.
|
||||
//
|
||||
// Even more fine-grained information than Debug events.
|
||||
// Loggers not supporting this level should fall back to Debug.
|
||||
Trace(msg string, keyvals ...interface{})
|
||||
|
||||
// Debug logs a Debug event.
|
||||
//
|
||||
// A verbose series of information events.
|
||||
// They are useful when debugging the system.
|
||||
Debug(msg string, keyvals ...interface{})
|
||||
|
||||
// Info logs an Info event.
|
||||
//
|
||||
// General information about what's happening inside the system.
|
||||
Info(msg string, keyvals ...interface{})
|
||||
|
||||
// Warn logs a Warn(ing) event.
|
||||
//
|
||||
// Non-critical events that should be looked at.
|
||||
Warn(msg string, keyvals ...interface{})
|
||||
|
||||
// Error logs an Error event.
|
||||
//
|
||||
// Critical events that require immediate attention.
|
||||
// Loggers commonly provide Fatal and Panic levels above Error level,
|
||||
// but exiting and panicing is out of scope for a logging library.
|
||||
Error(msg string, keyvals ...interface{})
|
||||
}
|
||||
|
||||
type jwwLogger struct{}
|
||||
|
||||
func (jwwLogger) Trace(msg string, keyvals ...interface{}) {
|
||||
jww.TRACE.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Debug(msg string, keyvals ...interface{}) {
|
||||
jww.DEBUG.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Info(msg string, keyvals ...interface{}) {
|
||||
jww.INFO.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Warn(msg string, keyvals ...interface{}) {
|
||||
jww.WARN.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Error(msg string, keyvals ...interface{}) {
|
||||
jww.ERROR.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func jwwLogMessage(msg string, keyvals ...interface{}) string {
|
||||
out := msg
|
||||
|
||||
if len(keyvals) > 0 && len(keyvals)%2 == 1 {
|
||||
keyvals = append(keyvals, nil)
|
||||
}
|
||||
|
||||
for i := 0; i <= len(keyvals)-2; i += 2 {
|
||||
out = fmt.Sprintf("%s %v=%v", out, keyvals[i], keyvals[i+1])
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
|
@ -78,6 +78,7 @@ func TestNestedOverrides(t *testing.T) {
|
|||
func overrideDefault(assert *assert.Assertions, firstPath string, firstValue interface{}, secondPath string, secondValue interface{}) *Viper {
|
||||
return overrideFromLayer(defaultLayer, assert, firstPath, firstValue, secondPath, secondValue)
|
||||
}
|
||||
|
||||
func override(assert *assert.Assertions, firstPath string, firstValue interface{}, secondPath string, secondValue interface{}) *Viper {
|
||||
return overrideFromLayer(overrideLayer, assert, firstPath, firstValue, secondPath, secondValue)
|
||||
}
|
||||
|
|
|
@ -10,10 +10,11 @@ import (
|
|||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/ShaleApps/viper"
|
||||
crypt "github.com/sagikazarmark/crypt/config"
|
||||
|
||||
crypt "github.com/xordataexchange/crypt/config"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type remoteConfigProvider struct{}
|
||||
|
@ -75,6 +76,7 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
|||
var cm crypt.ConfigManager
|
||||
var err error
|
||||
|
||||
endpoints := strings.Split(rp.Endpoint(), ";")
|
||||
if rp.SecretKeyring() != "" {
|
||||
var kr *os.File
|
||||
kr, err = os.Open(rp.SecretKeyring())
|
||||
|
@ -82,16 +84,26 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
|||
return nil, err
|
||||
}
|
||||
defer kr.Close()
|
||||
if rp.Provider() == "etcd" {
|
||||
cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr)
|
||||
} else {
|
||||
cm, err = crypt.NewConsulConfigManager([]string{rp.Endpoint()}, kr)
|
||||
switch rp.Provider() {
|
||||
case "etcd":
|
||||
cm, err = crypt.NewEtcdConfigManager(endpoints, kr)
|
||||
case "etcd3":
|
||||
cm, err = crypt.NewEtcdV3ConfigManager(endpoints, kr)
|
||||
case "firestore":
|
||||
cm, err = crypt.NewFirestoreConfigManager(endpoints, kr)
|
||||
default:
|
||||
cm, err = crypt.NewConsulConfigManager(endpoints, kr)
|
||||
}
|
||||
} else {
|
||||
if rp.Provider() == "etcd" {
|
||||
cm, err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()})
|
||||
} else {
|
||||
cm, err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()})
|
||||
switch rp.Provider() {
|
||||
case "etcd":
|
||||
cm, err = crypt.NewStandardEtcdConfigManager(endpoints)
|
||||
case "etcd3":
|
||||
cm, err = crypt.NewStandardEtcdV3ConfigManager(endpoints)
|
||||
case "firestore":
|
||||
cm, err = crypt.NewStandardFirestoreConfigManager(endpoints)
|
||||
default:
|
||||
cm, err = crypt.NewStandardConsulConfigManager(endpoints)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
|
|
62
util.go
62
util.go
|
@ -18,9 +18,7 @@ import (
|
|||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/cast"
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
// ConfigParseError denotes failing to parse configuration file.
|
||||
|
@ -66,18 +64,25 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
|
|||
return nm
|
||||
}
|
||||
|
||||
func insensitiviseVal(val interface{}) interface{} {
|
||||
switch val.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
// nested map: cast and recursively insensitivise
|
||||
val = cast.ToStringMap(val)
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
case map[string]interface{}:
|
||||
// nested map: recursively insensitivise
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
case []interface{}:
|
||||
// nested array: recursively insensitivise
|
||||
insensitiveArray(val.([]interface{}))
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func insensitiviseMap(m map[string]interface{}) {
|
||||
for key, val := range m {
|
||||
switch val.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
// nested map: cast and recursively insensitivise
|
||||
val = cast.ToStringMap(val)
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
case map[string]interface{}:
|
||||
// nested map: recursively insensitivise
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
}
|
||||
|
||||
val = insensitiviseVal(val)
|
||||
lower := strings.ToLower(key)
|
||||
if key != lower {
|
||||
// remove old key (not lower-cased)
|
||||
|
@ -88,17 +93,20 @@ func insensitiviseMap(m map[string]interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func absPathify(inPath string) string {
|
||||
jww.INFO.Println("Trying to resolve absolute path to", inPath)
|
||||
func insensitiveArray(a []interface{}) {
|
||||
for i, val := range a {
|
||||
a[i] = insensitiviseVal(val)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(inPath, "$HOME") {
|
||||
func absPathify(logger Logger, inPath string) string {
|
||||
logger.Info("trying to resolve absolute path", "path", inPath)
|
||||
|
||||
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
|
||||
inPath = userHomeDir() + inPath[5:]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(inPath, "$") {
|
||||
end := strings.Index(inPath, string(os.PathSeparator))
|
||||
inPath = os.Getenv(inPath[1:end]) + inPath[end:]
|
||||
}
|
||||
inPath = os.ExpandEnv(inPath)
|
||||
|
||||
if filepath.IsAbs(inPath) {
|
||||
return filepath.Clean(inPath)
|
||||
|
@ -109,21 +117,9 @@ func absPathify(inPath string) string {
|
|||
return filepath.Clean(p)
|
||||
}
|
||||
|
||||
jww.ERROR.Println("Couldn't discover absolute path")
|
||||
jww.ERROR.Println(err)
|
||||
return ""
|
||||
}
|
||||
logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
|
||||
|
||||
// Check if file Exists
|
||||
func exists(fs afero.Fs, path string) (bool, error) {
|
||||
stat, err := fs.Stat(path)
|
||||
if err == nil {
|
||||
return !stat.IsDir(), nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
return ""
|
||||
}
|
||||
|
||||
func stringInSlice(a string, list []string) bool {
|
||||
|
|
53
util_test.go
53
util_test.go
|
@ -11,25 +11,29 @@
|
|||
package viper
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper/internal/testutil"
|
||||
)
|
||||
|
||||
func TestCopyAndInsensitiviseMap(t *testing.T) {
|
||||
var (
|
||||
given = map[string]interface{}{
|
||||
"Foo": 32,
|
||||
"Bar": map[interface{}]interface {
|
||||
}{
|
||||
"Bar": map[interface{}]interface{}{
|
||||
"ABc": "A",
|
||||
"cDE": "B"},
|
||||
"cDE": "B",
|
||||
},
|
||||
}
|
||||
expected = map[string]interface{}{
|
||||
"foo": 32,
|
||||
"bar": map[string]interface {
|
||||
}{
|
||||
"bar": map[string]interface{}{
|
||||
"abc": "A",
|
||||
"cde": "B"},
|
||||
"cde": "B",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -52,3 +56,40 @@ func TestCopyAndInsensitiviseMap(t *testing.T) {
|
|||
t.Fatal("Input map changed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsPathify(t *testing.T) {
|
||||
skipWindows(t)
|
||||
|
||||
home := userHomeDir()
|
||||
homer := filepath.Join(home, "homer")
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
testutil.Setenv(t, "HOMER_ABSOLUTE_PATH", homer)
|
||||
testutil.Setenv(t, "VAR_WITH_RELATIVE_PATH", "relative")
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
output string
|
||||
}{
|
||||
{"", wd},
|
||||
{"sub", filepath.Join(wd, "sub")},
|
||||
{"./", wd},
|
||||
{"./sub", filepath.Join(wd, "sub")},
|
||||
{"$HOME", home},
|
||||
{"$HOME/", home},
|
||||
{"$HOME/sub", filepath.Join(home, "sub")},
|
||||
{"$HOMER_ABSOLUTE_PATH", homer},
|
||||
{"$HOMER_ABSOLUTE_PATH/", homer},
|
||||
{"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")},
|
||||
{"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")},
|
||||
{"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")},
|
||||
{"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got := absPathify(jwwLogger{}, test.input)
|
||||
if got != test.output {
|
||||
t.Errorf("Got %v\nexpected\n%q", got, test.output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
57
viper_go1_15.go
Normal file
57
viper_go1_15.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
//go:build !go1.16 || !finder
|
||||
// +build !go1.16 !finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// Search all configPaths for any config file.
|
||||
// Returns the first path that exists (and is a config file).
|
||||
func (v *Viper) findConfigFile() (string, error) {
|
||||
v.logger.Info("searching for config in paths", "paths", v.configPaths)
|
||||
|
||||
for _, cp := range v.configPaths {
|
||||
file := v.searchInPath(cp)
|
||||
if file != "" {
|
||||
return file, nil
|
||||
}
|
||||
}
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
}
|
||||
|
||||
func (v *Viper) searchInPath(in string) (filename string) {
|
||||
v.logger.Debug("searching for config in path", "path", in)
|
||||
for _, ext := range SupportedExts {
|
||||
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
|
||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
||||
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
|
||||
return filepath.Join(in, v.configName+"."+ext)
|
||||
}
|
||||
}
|
||||
|
||||
if v.configType != "" {
|
||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
|
||||
return filepath.Join(in, v.configName)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check if file Exists
|
||||
func exists(fs afero.Fs, path string) (bool, error) {
|
||||
stat, err := fs.Stat(path)
|
||||
if err == nil {
|
||||
return !stat.IsDir(), nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
32
viper_go1_16.go
Normal file
32
viper_go1_16.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
//go:build go1.16 && finder
|
||||
// +build go1.16,finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// Search all configPaths for any config file.
|
||||
// Returns the first path that exists (and is a config file).
|
||||
func (v *Viper) findConfigFile() (string, error) {
|
||||
finder := finder{
|
||||
paths: v.configPaths,
|
||||
fileNames: []string{v.configName},
|
||||
extensions: SupportedExts,
|
||||
withoutExtension: v.configType != "",
|
||||
}
|
||||
|
||||
file, err := finder.Find(afero.NewIOFS(v.fs))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if file == "" {
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
1059
viper_test.go
1059
viper_test.go
File diff suppressed because it is too large
Load diff
53
viper_yaml_test.go
Normal file
53
viper_yaml_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package viper
|
||||
|
||||
var yamlExample = []byte(`Hacker: true
|
||||
name: steve
|
||||
hobbies:
|
||||
- skateboarding
|
||||
- snowboarding
|
||||
- go
|
||||
clothing:
|
||||
jacket: leather
|
||||
trousers: denim
|
||||
pants:
|
||||
size: large
|
||||
age: 35
|
||||
eyes : brown
|
||||
beard: true
|
||||
`)
|
||||
|
||||
var yamlWriteExpected = []byte(`age: 35
|
||||
beard: true
|
||||
clothing:
|
||||
jacket: leather
|
||||
pants:
|
||||
size: large
|
||||
trousers: denim
|
||||
eyes: brown
|
||||
hacker: true
|
||||
hobbies:
|
||||
- skateboarding
|
||||
- snowboarding
|
||||
- go
|
||||
name: steve
|
||||
`)
|
||||
|
||||
var yamlExampleWithDot = []byte(`Hacker: true
|
||||
name: steve
|
||||
hobbies:
|
||||
- skateboarding
|
||||
- snowboarding
|
||||
- go
|
||||
clothing:
|
||||
jacket: leather
|
||||
trousers: denim
|
||||
pants:
|
||||
size: large
|
||||
age: 35
|
||||
eyes : brown
|
||||
beard: true
|
||||
emails:
|
||||
steve@hacker.com:
|
||||
created: 01/02/03
|
||||
active: true
|
||||
`)
|
12
watch.go
Normal file
12
watch.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
//go:build darwin || dragonfly || freebsd || openbsd || linux || netbsd || solaris || windows
|
||||
// +build darwin dragonfly freebsd openbsd linux netbsd solaris windows
|
||||
|
||||
package viper
|
||||
|
||||
import "github.com/fsnotify/fsnotify"
|
||||
|
||||
type watcher = fsnotify.Watcher
|
||||
|
||||
func newWatcher() (*watcher, error) {
|
||||
return fsnotify.NewWatcher()
|
||||
}
|
32
watch_unsupported.go
Normal file
32
watch_unsupported.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
//go:build appengine || (!darwin && !dragonfly && !freebsd && !openbsd && !linux && !netbsd && !solaris && !windows)
|
||||
// +build appengine !darwin,!dragonfly,!freebsd,!openbsd,!linux,!netbsd,!solaris,!windows
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
func newWatcher() (*watcher, error) {
|
||||
return &watcher{}, fmt.Errorf("fsnotify not supported on %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
type watcher struct {
|
||||
Events chan fsnotify.Event
|
||||
Errors chan error
|
||||
}
|
||||
|
||||
func (*watcher) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*watcher) Add(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*watcher) Remove(name string) error {
|
||||
return nil
|
||||
}
|
Loading…
Add table
Reference in a new issue