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

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"go/types"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/niko0xdev/gqlgen/internal/code"
    10  )
    11  
    12  type ResolverConfig struct {
    13  	Filename            string         `yaml:"filename,omitempty"`
    14  	FilenameTemplate    string         `yaml:"filename_template,omitempty"`
    15  	Package             string         `yaml:"package,omitempty"`
    16  	Type                string         `yaml:"type,omitempty"`
    17  	Layout              ResolverLayout `yaml:"layout,omitempty"`
    18  	DirName             string         `yaml:"dir"`
    19  	OmitTemplateComment bool           `yaml:"omit_template_comment,omitempty"`
    20  	ResolverTemplate    string         `yaml:"resolver_template,omitempty"`
    21  }
    22  
    23  type ResolverLayout string
    24  
    25  var (
    26  	LayoutSingleFile   ResolverLayout = "single-file"
    27  	LayoutFollowSchema ResolverLayout = "follow-schema"
    28  )
    29  
    30  func (r *ResolverConfig) Check() error {
    31  	if r.Layout == "" {
    32  		r.Layout = LayoutSingleFile
    33  	}
    34  	if r.Type == "" {
    35  		r.Type = "Resolver"
    36  	}
    37  
    38  	switch r.Layout {
    39  	case LayoutSingleFile:
    40  		if r.Filename == "" {
    41  			return fmt.Errorf("filename must be specified with layout=%s", r.Layout)
    42  		}
    43  		if !strings.HasSuffix(r.Filename, ".go") {
    44  			return fmt.Errorf("filename should be path to a go source file with layout=%s", r.Layout)
    45  		}
    46  		r.Filename = abs(r.Filename)
    47  	case LayoutFollowSchema:
    48  		if r.DirName == "" {
    49  			return fmt.Errorf("dirname must be specified with layout=%s", r.Layout)
    50  		}
    51  		r.DirName = abs(r.DirName)
    52  		if r.Filename == "" {
    53  			r.Filename = filepath.Join(r.DirName, "resolver.go")
    54  		} else {
    55  			r.Filename = abs(r.Filename)
    56  		}
    57  	default:
    58  		return fmt.Errorf("invalid layout %s. must be %s or %s", r.Layout, LayoutSingleFile, LayoutFollowSchema)
    59  	}
    60  
    61  	if strings.ContainsAny(r.Package, "./\\") {
    62  		return fmt.Errorf("package should be the output package name only, do not include the output filename")
    63  	}
    64  
    65  	if r.Package == "" && r.Dir() != "" {
    66  		r.Package = code.NameForDir(r.Dir())
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func (r *ResolverConfig) ImportPath() string {
    73  	if r.Dir() == "" {
    74  		return ""
    75  	}
    76  	return code.ImportPathForDir(r.Dir())
    77  }
    78  
    79  func (r *ResolverConfig) Dir() string {
    80  	switch r.Layout {
    81  	case LayoutSingleFile:
    82  		if r.Filename == "" {
    83  			return ""
    84  		}
    85  		return filepath.Dir(r.Filename)
    86  	case LayoutFollowSchema:
    87  		return r.DirName
    88  	default:
    89  		panic("invalid layout " + r.Layout)
    90  	}
    91  }
    92  
    93  func (r *ResolverConfig) Pkg() *types.Package {
    94  	if r.Dir() == "" {
    95  		return nil
    96  	}
    97  	return types.NewPackage(r.ImportPath(), r.Package)
    98  }
    99  
   100  func (r *ResolverConfig) IsDefined() bool {
   101  	return r.Filename != "" || r.DirName != ""
   102  }