go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configutil/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  )
    13  
    14  type fullConfig struct {
    15  	fullConfigMeta `yaml:",inline"`
    16  
    17  	Field0 string `json:"field0" yaml:"field0"`
    18  	Field1 string `json:"field1" yaml:"field1"`
    19  	Field2 string `json:"field2" yaml:"field2"`
    20  	Field3 string `json:"field3" yaml:"field3"`
    21  
    22  	Child fullConfigChild `json:"child" yaml:"child"`
    23  }
    24  
    25  func (fc *fullConfig) Resolve(ctx context.Context) error {
    26  	return Resolve(ctx,
    27  		(&fc.fullConfigMeta).Resolve,
    28  		(&fc.Child).Resolve,
    29  
    30  		Set(&fc.Field0, Env[string]("CONFIGUTIL_FIELD0"), Lazy(&fc.Field0), Const("default-field0")),
    31  		Set(&fc.Field1, Env[string]("CONFIGUTIL_FIELD1"), Lazy(&fc.Field1), Const("default-field1")),
    32  		Set(&fc.Field2, Env[string]("CONFIGUTIL_FIELD2"), Lazy(&fc.Field2), Const("default-field2")),
    33  		Set(&fc.Field3, Env[string]("CONFIGUTIL_FIELD3"), Lazy(&fc.Field3), Const("default-field3")),
    34  	)
    35  }
    36  
    37  type fullConfigMeta struct {
    38  	ServiceName string `json:"serviceName" yaml:"serviceName"`
    39  	ServiceEnv  string `json:"serviceEnv" yaml:"serviceEnv"`
    40  	Version     string `json:"version" yaml:"version"`
    41  }
    42  
    43  func (fcm *fullConfigMeta) Resolve(ctx context.Context) error {
    44  	return Resolve(ctx,
    45  		Set(&fcm.ServiceEnv, Env[string]("SERVICE_ENV"), Lazy(&fcm.ServiceEnv), Const("dev")),
    46  		Set(&fcm.ServiceName, Env[string]("SERVICE_NAME"), Lazy(&fcm.ServiceName), Const("configutil")),
    47  		Set(&fcm.Version, Env[string]("VERSION"), Lazy(&fcm.Version), Const("0.1.0")),
    48  	)
    49  }
    50  
    51  type fullConfigChild struct {
    52  	Field0 string `json:"field0" yaml:"field0"`
    53  	Field1 string `json:"field1" yaml:"field1"`
    54  	Field2 string `json:"field2" yaml:"field2"`
    55  	Field3 string `json:"field3" yaml:"field3"`
    56  }
    57  
    58  func (fcc *fullConfigChild) Resolve(ctx context.Context) error {
    59  	return Resolve(ctx,
    60  		Set(&fcc.Field0, Env[string]("CONFIGUTIL_CHILD_FIELD0"), Lazy(&fcc.Field0), Const("default-child-field0")),
    61  		Set(&fcc.Field1, Env[string]("CONFIGUTIL_CHILD_FIELD1"), Lazy(&fcc.Field1), Const("default-child-field1")),
    62  		Set(&fcc.Field2, Env[string]("CONFIGUTIL_CHILD_FIELD2"), Lazy(&fcc.Field2), Const("default-child-field2")),
    63  		Set(&fcc.Field3, Env[string]("CONFIGUTIL_CHILD_FIELD3"), Lazy(&fcc.Field3), Const("default-child-field3")),
    64  	)
    65  }