github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/codegen/config/initialisms.go (about)

     1  package config
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/niko0xdev/gqlgen/codegen/templates"
     7  )
     8  
     9  // GoInitialismsConfig allows to modify the default behavior of naming Go methods, types and properties
    10  type GoInitialismsConfig struct {
    11  	// If true, the Initialisms won't get appended to the default ones but replace them
    12  	ReplaceDefaults bool `yaml:"replace_defaults"`
    13  	// Custom initialisms to be added or to replace the default ones
    14  	Initialisms []string `yaml:"initialisms"`
    15  }
    16  
    17  // setInitialisms adjustes GetInitialisms based on its settings.
    18  func (i GoInitialismsConfig) setInitialisms() {
    19  	toUse := i.determineGoInitialisms()
    20  	templates.GetInitialisms = func() map[string]bool {
    21  		return toUse
    22  	}
    23  }
    24  
    25  // determineGoInitialisms returns the Go initialims to be used, based on its settings.
    26  func (i GoInitialismsConfig) determineGoInitialisms() (initialismsToUse map[string]bool) {
    27  	if i.ReplaceDefaults {
    28  		initialismsToUse = make(map[string]bool, len(i.Initialisms))
    29  		for _, initialism := range i.Initialisms {
    30  			initialismsToUse[strings.ToUpper(initialism)] = true
    31  		}
    32  	} else {
    33  		initialismsToUse = make(map[string]bool, len(templates.CommonInitialisms)+len(i.Initialisms))
    34  		for initialism, value := range templates.CommonInitialisms {
    35  			initialismsToUse[strings.ToUpper(initialism)] = value
    36  		}
    37  		for _, initialism := range i.Initialisms {
    38  			initialismsToUse[strings.ToUpper(initialism)] = true
    39  		}
    40  	}
    41  	return initialismsToUse
    42  }