...

Source file src/github.com/ory/x/configx/koanf_schema_defaults.go

Documentation: github.com/ory/x/configx

     1  package configx
     2  
     3  import (
     4  	"github.com/knadh/koanf/maps"
     5  	"github.com/pkg/errors"
     6  
     7  	"github.com/ory/jsonschema/v3"
     8  	"github.com/ory/x/jsonschemax"
     9  )
    10  
    11  type KoanfSchemaDefaults struct {
    12  	c   *jsonschema.Compiler
    13  	uri string
    14  }
    15  
    16  func NewKoanfSchemaDefaults(schema []byte) (*KoanfSchemaDefaults, error) {
    17  	id, c, err := newCompiler(schema)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	return &KoanfSchemaDefaults{c: c, uri: id}, nil
    22  }
    23  
    24  func (k *KoanfSchemaDefaults) ReadBytes() ([]byte, error) {
    25  	return nil, errors.New("schema defaults provider does not support this method")
    26  }
    27  
    28  func (k *KoanfSchemaDefaults) Read() (map[string]interface{}, error) {
    29  	keys, err := jsonschemax.ListPaths(k.uri, k.c)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	values := map[string]interface{}{}
    35  	for _, key := range keys {
    36  		if key.Default != nil {
    37  			values[key.Name] = key.Default
    38  		}
    39  	}
    40  
    41  	return maps.Unflatten(values, "."), nil
    42  }
    43  

View as plain text