github.com/kaydxh/golang@v0.0.131/pkg/config/config.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package config
    23  
    24  import (
    25  	"github.com/go-playground/validator/v10"
    26  	viper_ "github.com/kaydxh/golang/pkg/viper"
    27  	"github.com/spf13/viper"
    28  
    29  	"google.golang.org/protobuf/proto"
    30  )
    31  
    32  // proto.Message is interface
    33  type Config[T proto.Message] struct {
    34  	Proto     T
    35  	Validator *validator.Validate
    36  
    37  	opts struct {
    38  		viper *viper.Viper
    39  	}
    40  }
    41  
    42  type completedConfig[T proto.Message] struct {
    43  	*Config[T]
    44  	completeError error
    45  }
    46  
    47  // CompletedConfig same as Config, just to swap private object.
    48  type CompletedConfig[T proto.Message] struct {
    49  	// Embed a private pointer that cannot be instantiated outside of this package.
    50  	*completedConfig[T]
    51  }
    52  
    53  // Validate checks Config.
    54  func (c *completedConfig[T]) Validate() error {
    55  	return c.Validator.Struct(c)
    56  }
    57  
    58  // Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
    59  func (c *Config[T]) Complete() CompletedConfig[T] {
    60  	err := c.loadViper()
    61  	if err != nil {
    62  		return CompletedConfig[T]{
    63  			&completedConfig[T]{
    64  				Config:        c,
    65  				completeError: err,
    66  			}}
    67  	}
    68  
    69  	if c.Validator == nil {
    70  		c.Validator = validator.New()
    71  	}
    72  
    73  	return CompletedConfig[T]{&completedConfig[T]{Config: c}}
    74  }
    75  
    76  func (c *Config[T]) loadViper() error {
    77  	if c.opts.viper != nil {
    78  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, c.Proto)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func (c completedConfig[T]) New() (T, error) {
    85  	var zero T
    86  	if c.completeError != nil {
    87  		return zero, c.completeError
    88  	}
    89  
    90  	return c.Proto, nil
    91  }
    92  
    93  func NewConfig[T proto.Message](value T, options ...ConfigOption[T]) *Config[T] {
    94  	var c Config[T]
    95  	c.Proto = value
    96  	c.ApplyOptions(options...)
    97  
    98  	return &c
    99  }