compositeConfig.go (2872B)
1 // Copyright 2021 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package config
15
16 import (
17 "github.com/gohugoio/hugo/common/maps"
18 )
19
20 // NewCompositeConfig creates a new composite Provider with a read-only base
21 // and a writeable layer.
22 func NewCompositeConfig(base, layer Provider) Provider {
23 return &compositeConfig{
24 base: base,
25 layer: layer,
26 }
27 }
28
29 // compositeConfig contains a read only config base with
30 // a possibly writeable config layer on top.
31 type compositeConfig struct {
32 base Provider
33 layer Provider
34 }
35
36 func (c *compositeConfig) GetBool(key string) bool {
37 if c.layer.IsSet(key) {
38 return c.layer.GetBool(key)
39 }
40 return c.base.GetBool(key)
41 }
42
43 func (c *compositeConfig) GetInt(key string) int {
44 if c.layer.IsSet(key) {
45 return c.layer.GetInt(key)
46 }
47 return c.base.GetInt(key)
48 }
49
50 func (c *compositeConfig) Merge(key string, value any) {
51 c.layer.Merge(key, value)
52 }
53
54 func (c *compositeConfig) GetParams(key string) maps.Params {
55 if c.layer.IsSet(key) {
56 return c.layer.GetParams(key)
57 }
58 return c.base.GetParams(key)
59 }
60
61 func (c *compositeConfig) GetStringMap(key string) map[string]any {
62 if c.layer.IsSet(key) {
63 return c.layer.GetStringMap(key)
64 }
65 return c.base.GetStringMap(key)
66 }
67
68 func (c *compositeConfig) GetStringMapString(key string) map[string]string {
69 if c.layer.IsSet(key) {
70 return c.layer.GetStringMapString(key)
71 }
72 return c.base.GetStringMapString(key)
73 }
74
75 func (c *compositeConfig) GetStringSlice(key string) []string {
76 if c.layer.IsSet(key) {
77 return c.layer.GetStringSlice(key)
78 }
79 return c.base.GetStringSlice(key)
80 }
81
82 func (c *compositeConfig) Get(key string) any {
83 if c.layer.IsSet(key) {
84 return c.layer.Get(key)
85 }
86 return c.base.Get(key)
87 }
88
89 func (c *compositeConfig) IsSet(key string) bool {
90 if c.layer.IsSet(key) {
91 return true
92 }
93 return c.base.IsSet(key)
94 }
95
96 func (c *compositeConfig) GetString(key string) string {
97 if c.layer.IsSet(key) {
98 return c.layer.GetString(key)
99 }
100 return c.base.GetString(key)
101 }
102
103 func (c *compositeConfig) Set(key string, value any) {
104 c.layer.Set(key, value)
105 }
106
107 func (c *compositeConfig) SetDefaults(params maps.Params) {
108 c.layer.SetDefaults(params)
109 }
110
111 func (c *compositeConfig) WalkParams(walkFn func(params ...KeyParams) bool) {
112 panic("not supported")
113 }
114
115 func (c *compositeConfig) SetDefaultMergeStrategy() {
116 panic("not supported")
117 }