Skip to content

Commit c76e8aa

Browse files
committed
feat: implement custom JSON unmarshalling for EnvVarCfg to support string and object formats
Signed-off-by: Andrew Hemming <andrew@footprintit.net>
1 parent f196329 commit c76e8aa

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

api/v1alpha1/mcpserver_types.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"encoding/json"
21+
2022
corev1 "k8s.io/api/core/v1"
2123
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
)
@@ -259,6 +261,27 @@ type EnvVarCfg struct {
259261
ValueFrom *corev1.EnvVarSource `json:"valueFrom,omitempty"`
260262
}
261263

264+
// UnmarshalJSON implements json.Unmarshaler for EnvVarCfg.
265+
// It allows unmarshalling a plain string into the Value field, or an object into Value/ValueFrom.
266+
func (e *EnvVarCfg) UnmarshalJSON(data []byte) error {
267+
// Try to unmarshal as a string
268+
var str string
269+
if err := json.Unmarshal(data, &str); err == nil {
270+
e.Value = str
271+
return nil
272+
}
273+
274+
// If not a string, try to unmarshal as an object
275+
type rawEnvVarCfg EnvVarCfg // Use a new type to avoid infinite recursion
276+
var raw rawEnvVarCfg
277+
if err := json.Unmarshal(data, &raw); err != nil {
278+
return err
279+
}
280+
e.Value = raw.Value
281+
e.ValueFrom = raw.ValueFrom
282+
return nil
283+
}
284+
262285
// InitContainerConfig defines the configuration for the init container.
263286
type InitContainerConfig struct {
264287
// Image defines the full image reference for the init container.

0 commit comments

Comments
 (0)