github.com/haraldrudell/parl@v0.4.176/yamlo/add-yaml-references.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package yamlo
     7  
     8  import (
     9  	"github.com/haraldrudell/parl/perrors"
    10  	"github.com/haraldrudell/parl/pflags"
    11  )
    12  
    13  // AddYamlReferences returns a [pflags.OptionData] slice of options to main
    14  //   - return value is appended to the [pflags.OptionData] list in main
    15  //   - filedPointerValues is a list of references to main’s y YamlData
    16  //   - subPackageOptionData is a list of options declared in subpackage
    17  func AddYamlReferences(
    18  	subPackageOptionData []pflags.OptionData,
    19  	fieldPointerValues []interface{},
    20  ) (optionDataList []pflags.OptionData) {
    21  
    22  	// verify main and subPackage integrity
    23  	if len(fieldPointerValues) != len(subPackageOptionData) {
    24  		panic(perrors.ErrorfPF("length mismatch: main.fieldPointerValues: %d subPackageOptionData: %d",
    25  			len(fieldPointerValues), len(subPackageOptionData),
    26  		))
    27  	}
    28  
    29  	// link subPackageOptionData to main’s yaml
    30  	for i := 0; i < len(subPackageOptionData); i++ {
    31  
    32  		// get the reference to the field of main’s y YamlData object for this subPackageOptionData option
    33  		var yReference = fieldPointerValues[i]
    34  		if yReference == nil {
    35  			continue // this option is not in yaml
    36  		}
    37  
    38  		// update subPackageOptionData so that [ApplyYaml] can update
    39  		// this option value with values read from yaml
    40  		subPackageOptionData[i].Y = yReference
    41  	}
    42  
    43  	optionDataList = subPackageOptionData
    44  
    45  	return
    46  }