conversion

package
v0.0.0-...-fca02c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package conversion provides tools to systematically convert Go data structures into GraphQL schema elements including types and fields, based on the type information extracted from Go source files using functions from package github.com/VintageOps/structogqlgen/pkg/load

Index

Constants

View Source
const (
	InvalidTypeErr = ConvertCustomError("invalid type")
)

InvalidTypeErr represents an error indicating an invalid type.

Variables

View Source
var MapBasicKindToGqlType = map[types.BasicKind]gqlTypeIsCustScalar{
	types.Bool:           {/* contains filtered or unexported fields */},
	types.Int:            {/* contains filtered or unexported fields */},
	types.Int8:           {/* contains filtered or unexported fields */},
	types.Int16:          {/* contains filtered or unexported fields */},
	types.Int32:          {/* contains filtered or unexported fields */},
	types.Int64:          {/* contains filtered or unexported fields */},
	types.Uint:           {/* contains filtered or unexported fields */},
	types.Uint8:          {/* contains filtered or unexported fields */},
	types.Uint16:         {/* contains filtered or unexported fields */},
	types.Uint32:         {/* contains filtered or unexported fields */},
	types.Uint64:         {/* contains filtered or unexported fields */},
	types.Uintptr:        {/* contains filtered or unexported fields */},
	types.Float32:        {/* contains filtered or unexported fields */},
	types.Float64:        {/* contains filtered or unexported fields */},
	types.Complex64:      {/* contains filtered or unexported fields */},
	types.Complex128:     {/* contains filtered or unexported fields */},
	types.String:         {/* contains filtered or unexported fields */},
	types.UnsafePointer:  {/* contains filtered or unexported fields */},
	types.UntypedBool:    {/* contains filtered or unexported fields */},
	types.UntypedInt:     {/* contains filtered or unexported fields */},
	types.UntypedRune:    {/* contains filtered or unexported fields */},
	types.UntypedFloat:   {/* contains filtered or unexported fields */},
	types.UntypedComplex: {/* contains filtered or unexported fields */},
	types.UntypedString:  {/* contains filtered or unexported fields */},
	types.UntypedNil:     {/* contains filtered or unexported fields */},
}

MapBasicKindToGqlType maps Go basic types to corresponding GraphQL types and indicates if that types need a custom scalar. Mirrors: https://pkg.go.dev/go/types#BasicKind

Functions

func ConvertType

func ConvertType(goType types.Type, gqlFieldDef *GqlFieldsDefinition) error

ConvertType converts a Go type into a GqlFieldsDefinition by performing type-specific conversions. It handles basic types, slices, pointers, maps, named types, and interfaces. .

func GqlPrettyPrint

func GqlPrettyPrint(gqlTypeDefs []GqlTypeDefinition, opts *PrettyPrintOptions) (string, error)

GqlPrettyPrint takes a slice of GqlTypeDefinition and PrettyPrintOptions and returns a string representation of the GraphQL type definitions.

Types

type ConvertCustomError

type ConvertCustomError string

ConvertCustomError represents a custom error type that can be used in Go programs.

func (ConvertCustomError) Error

func (e ConvertCustomError) Error() string

type GqlFieldsDefinition

type GqlFieldsDefinition struct {
	GqlFieldName         string                // GqlFieldName represents the name of a graphQL field
	GqlFieldType         string                // GqlFieldType is a string representing the type of GraphQL field
	GqlFieldTags         string                // GqlFieldTags represents the tags of a GraphQL field
	GqlFieldIsEmbedded   bool                  // GqlFieldIsEmbedded represents whether a GraphQL field is an embedded field.
	IsCustomScalar       bool                  // IsCustomScalar is True if this field need to define a Scalar which will be type Name
	NestedCustomType     []GqlTypeDefinition   // NestedCustomType represents any custom types that might be needed to be defined for this type.
	GqlGenFieldsEmbedded []GqlFieldsDefinition // GqlGenFieldsEmbedded represents fields for Embedded Structs
}

GqlFieldsDefinition represents the definition of a GraphQL field.

type GqlTypeDefinition

type GqlTypeDefinition struct {
	GqlTypeName string                // GqlTypeName is the name of a graphQL type.
	GqlFields   []GqlFieldsDefinition // GqlFields is a slice of GqlFieldsDefinition, which represents the fields of a GraphQL type.
}

GqlTypeDefinition contains the definition of a graphQl Type

func BuildGqlTypes

func BuildGqlTypes(structsFound []load.StructDiscovered) ([]GqlTypeDefinition, error)

BuildGqlTypes builds an array of GqlTypeDefinitions for a given array of struct definitions. It calls BuildGqlgenType for each struct definition and populates the array with the results. If any error occurs during the process, it returns the error immediately.

func BuildGqlgenType

func BuildGqlgenType(structDef load.StructDiscovered) (GqlTypeDefinition, error)

BuildGqlgenType builds a GqlTypeDefinition for a given struct definition. It converts the struct fields into GqlFieldsDefinition, populating the field name and tags. It also determines the field type by invoking ConvertType and handles any custom types or scalars.

type PrettyPrintOptions

type PrettyPrintOptions struct {
	UseJsonTags      bool
	UseCustomTags    string
	TagFieldToIgnore *string
	RequireTags      SpecTagRequire
}

PrettyPrintOptions represents the options for pretty-printing. It contains the following fields: - UseJsonTags: a bool indicating whether to use JSON tags - UseCustomTags: a string indicating the custom tags to use - RequireTags: a SpecTagRequire struct that specifies required tags

type SpecTagRequire

type SpecTagRequire struct {
	Key string
	Val string
}

SpecTagRequire defines the structure for specifying required tags.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL