github.com/Rookout/GoSDK@v0.1.48/pkg/config/object_dump_config.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  func GetObjectDumpConfig(key string) (ObjectDumpConfig, bool) {
     8  	defaults := config.Load().(DynamicConfiguration).ObjectDumpConfigDefaults
     9  	switch key {
    10  	case "strict":
    11  		return defaults.strictConfig, true
    12  	case "default":
    13  		return defaults.defaultConfig, true
    14  	case "tolerant":
    15  		return defaults.tolerantConfig, true
    16  	default:
    17  		return ObjectDumpConfig{}, false
    18  	}
    19  }
    20  
    21  func GetDefaultDumpConfig() ObjectDumpConfig {
    22  	c := config.Load().(DynamicConfiguration)
    23  	return c.ObjectDumpConfigDefaults.defaultConfig
    24  }
    25  
    26  type ObjectDumpConfig struct {
    27  	MaxDepth           int
    28  	MaxWidth           int
    29  	MaxCollectionDepth int
    30  	MaxString          int
    31  	ShouldTailor       bool
    32  	IsTailored         bool
    33  }
    34  
    35  func TailorObjectDumpConfig(kind reflect.Kind, objLen int) (o ObjectDumpConfig) {
    36  	defer func() {
    37  		o.IsTailored = true
    38  		o.ShouldTailor = false
    39  	}()
    40  
    41  	defaults := config.Load().(DynamicConfiguration).ObjectDumpConfigDefaults
    42  	switch kind {
    43  	case reflect.String:
    44  		o.MaxString = defaults.unlimitedConfig.MaxString
    45  		o.MaxDepth = 1
    46  		return
    47  	case reflect.Array, reflect.Slice, reflect.Map:
    48  		if objLen > defaults.tolerantConfig.MaxWidth || objLen == 0 {
    49  			o.MaxDepth = defaults.defaultConfig.MaxDepth
    50  			o.MaxWidth = defaults.unlimitedConfig.MaxWidth
    51  			o.MaxCollectionDepth = defaults.defaultConfig.MaxCollectionDepth
    52  			o.MaxString = defaults.defaultConfig.MaxString
    53  			return
    54  		}
    55  	}
    56  	return defaults.tolerantConfig
    57  }
    58  
    59  func max(a, b int) int {
    60  	if a > b {
    61  		return a
    62  	}
    63  	return b
    64  }
    65  
    66  func MaxObjectDumpConfig(a, b ObjectDumpConfig) ObjectDumpConfig {
    67  	return ObjectDumpConfig{
    68  		MaxDepth:           max(a.MaxDepth, b.MaxDepth),
    69  		MaxCollectionDepth: max(a.MaxCollectionDepth, b.MaxCollectionDepth),
    70  		MaxWidth:           max(a.MaxWidth, b.MaxWidth),
    71  		MaxString:          max(a.MaxString, b.MaxString),
    72  		ShouldTailor:       a.ShouldTailor || b.ShouldTailor,
    73  		IsTailored:         false,
    74  	}
    75  }