github.com/blend/go-sdk@v1.20220411.3/configutil/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/env"
    16  )
    17  
    18  // TestMain is the testing entrypoint.
    19  func TestMain(m *testing.M) {
    20  	os.Exit(m.Run())
    21  }
    22  
    23  type config struct {
    24  	Environment string `json:"env" yaml:"env" env:"SERVICE_ENV"`
    25  	Other       string `json:"other" yaml:"other" env:"OTHER"`
    26  	Base        string `json:"base" yaml:"base"`
    27  }
    28  
    29  type resolvedConfig struct {
    30  	config
    31  }
    32  
    33  // Resolve implements configutil.BareResolver.
    34  func (r *resolvedConfig) Resolve(ctx context.Context) error {
    35  	r.Environment = env.GetVars(ctx).String("ENVIRONMENT")
    36  	return nil
    37  }
    38  
    39  type fullConfig struct {
    40  	fullConfigMeta `yaml:",inline"`
    41  
    42  	Field0 string `json:"field0" yaml:"field0"`
    43  	Field1 string `json:"field1" yaml:"field1"`
    44  	Field2 string `json:"field2" yaml:"field2"`
    45  	Field3 string `json:"field3" yaml:"field3"`
    46  
    47  	Child fullConfigChild `json:"child" yaml:"child"`
    48  }
    49  
    50  func (fc *fullConfig) Resolve(ctx context.Context) error {
    51  	return Resolve(ctx,
    52  		(&fc.fullConfigMeta).Resolve,
    53  		(&fc.Child).Resolve,
    54  
    55  		SetString(&fc.Field0, Env("CONFIGUTIL_FIELD0"), String(fc.Field0), String("default-field0")),
    56  		SetString(&fc.Field1, Env("CONFIGUTIL_FIELD1"), String(fc.Field1), String("default-field1")),
    57  		SetString(&fc.Field2, Env("CONFIGUTIL_FIELD2"), String(fc.Field2), String("default-field2")),
    58  		SetString(&fc.Field3, Env("CONFIGUTIL_FIELD3"), String(fc.Field3), String("default-field3")),
    59  	)
    60  }
    61  
    62  type fullConfigMeta struct {
    63  	ServiceName string `json:"serviceName" yaml:"serviceName"`
    64  	ServiceEnv  string `json:"serviceEnv" yaml:"serviceEnv"`
    65  	Version     string `json:"version" yaml:"version"`
    66  }
    67  
    68  func (fcm *fullConfigMeta) Resolve(ctx context.Context) error {
    69  	return Resolve(ctx,
    70  		SetString(&fcm.ServiceEnv, Env("SERVICE_ENV"), String(fcm.ServiceEnv), String("dev")),
    71  		SetString(&fcm.ServiceName, Env("SERVICE_NAME"), String(fcm.ServiceName), String("configutil")),
    72  		SetString(&fcm.Version, Env("VERSION"), String(fcm.Version), String("0.1.0")),
    73  	)
    74  }
    75  
    76  type fullConfigChild struct {
    77  	Field0 string `json:"field0" yaml:"field0"`
    78  	Field1 string `json:"field1" yaml:"field1"`
    79  	Field2 string `json:"field2" yaml:"field2"`
    80  	Field3 string `json:"field3" yaml:"field3"`
    81  }
    82  
    83  func (fcc *fullConfigChild) Resolve(ctx context.Context) error {
    84  	return Resolve(ctx,
    85  		SetString(&fcc.Field0, Env("CONFIGUTIL_CHILD_FIELD0"), String(fcc.Field0), String("default-child-field0")),
    86  		SetString(&fcc.Field1, Env("CONFIGUTIL_CHILD_FIELD1"), String(fcc.Field1), String("default-child-field1")),
    87  		SetString(&fcc.Field2, Env("CONFIGUTIL_CHILD_FIELD2"), String(fcc.Field2), String("default-child-field2")),
    88  		SetString(&fcc.Field3, Env("CONFIGUTIL_CHILD_FIELD3"), String(fcc.Field3), String("default-child-field3")),
    89  	)
    90  }