-
Notifications
You must be signed in to change notification settings - Fork 0
/
koanf_memory.go
51 lines (39 loc) · 1.05 KB
/
koanf_memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package configx
import (
"context"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/json"
"github.com/pkg/errors"
stdjson "encoding/json"
)
// KoanfMemory implements a KoanfMemory provider.
type KoanfMemory struct {
doc stdjson.RawMessage
ctx context.Context
parser koanf.Parser
}
// NewKoanfMemory returns a file provider.
func NewKoanfMemory(ctx context.Context, doc stdjson.RawMessage) *KoanfMemory {
return &KoanfMemory{
ctx: ctx,
doc: doc,
parser: json.Parser(),
}
}
func (f *KoanfMemory) SetDoc(doc stdjson.RawMessage) {
f.doc = doc
}
// ReadBytes reads the contents of a file on disk and returns the bytes.
func (f *KoanfMemory) ReadBytes() ([]byte, error) {
return nil, errors.New("file provider does not support this method")
}
// Read is not supported by the file provider.
func (f *KoanfMemory) Read() (map[string]interface{}, error) {
v, err := f.parser.Unmarshal(f.doc)
if err != nil {
return nil, errors.WithStack(err)
}
return v, nil
}