parser

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 10 Imported by: 2

README

gofile-parser

This library parses .go files and returns an object containing definitions found on it. It aims to be a friendly wrapper around go/ast.

Limitations:
  • Recognizes interfaces but it doesn't parse its methods.
  • Functions and constants are not processed. The initial goal of this library is to return type definitions like structs and maps.
  • May not work with dot . imports.

Usage

package main

import (
    parser "github.com/mxmauro/gofile-parser"
)

func main() {
    pf, err := parser.ParseFile(parser.ParseFileOptions{
        Filename:      "./main.go",
        ResolveModule: true,
    })
}

ParseFile, ParseDirectory & ParseText parses a go source code from a string or file(s).

ResolveModule tries to locate the project's go.mod in order to gather the module name it belongs to and subdirectory.

ResolveReferences tries to resolve references, for example, when one struct has a field pointing to another one.

To process a ParsedDeclaration, it is recommended to use switch v := pd.Type.(type) {, where pd references to some ParsedDeclaration, in order to know the real type of the declaration.

They can be:

  • ParsedNativeType
  • ParsedNonNativeType
  • ParsedStruct
  • ParsedStructField
  • ParsedInterface
  • ParsedMap
  • ParsedArray
  • ParsedPointer
  • ParsedChannel

The same logic applies when, for example, you want to know the type of object a ParsedPointer points to, or the key and value types of a ParsedMap.

LICENSE

See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIdentifierParts added in v1.0.1

func GetIdentifierParts(qualifiedIdentifier string) (packageName string, identifier string)

func IsNativeType

func IsNativeType(name string) bool

func IsPublic

func IsPublic(name string) bool

func ResolveReferences

func ResolveReferences(parsedFiles []*ParsedFile)

ResolveReferences tries to resolve all ParsedNonNativeType references

Types

type Module

type Module struct {
	Name   string
	SubDir string
}

func (*Module) FullName

func (m *Module) FullName() string

type ParseDirectoryOptions

type ParseDirectoryOptions struct {
	BaseDir          string
	ResolveModule    bool
	IncludeTestFiles bool
}

type ParseFileOptions

type ParseFileOptions struct {
	Filename      string
	ResolveModule bool
}

type ParseTextOptions

type ParseTextOptions struct {
	Content  string
	Filename string
	Module   Module
}

type ParsedArray

type ParsedArray struct {
	Size      string // Empty string means a slice, ellipsis is an array of items known at compile time, else a constant or a type
	ParsedInt *int64
	ValueType interface{}
}

type ParsedChannel

type ParsedChannel struct {
	Dir  ast.ChanDir
	Type interface{}
}

type ParsedDeclaration

type ParsedDeclaration struct {
	Name string
	Type interface{}
	Tags ParsedTags
}

type ParsedField added in v1.0.1

type ParsedField struct {
	Names        []string
	ImplicitName string
	Type         interface{}
	Tags         ParsedTags
}

func (*ParsedField) Identifiers added in v1.0.3

func (pf *ParsedField) Identifiers() []string

type ParsedFile

type ParsedFile struct {
	Module       Module
	Filename     string
	Package      string
	Imports      []ParsedImport
	Declarations []ParsedDeclaration
	// contains filtered or unexported fields
}

func ParseDirectory

func ParseDirectory(opts ParseDirectoryOptions) ([]*ParsedFile, error)

func ParseFile

func ParseFile(opts ParseFileOptions) (*ParsedFile, error)

func ParseText

func ParseText(opts ParseTextOptions) (*ParsedFile, error)

type ParsedFunction added in v1.0.1

type ParsedFunction struct {
	TypeParams []ParsedFunctionTypeParam
	Params     []ParsedFunctionParam
	Results    []ParsedFunctionResult
}

type ParsedFunctionParam added in v1.0.1

type ParsedFunctionParam = ParsedField

type ParsedFunctionResult added in v1.0.1

type ParsedFunctionResult = ParsedField

type ParsedFunctionTypeParam added in v1.0.1

type ParsedFunctionTypeParam = ParsedField

type ParsedImport added in v1.0.3

type ParsedImport struct {
	Name         string
	ImplicitName string
	Path         string
}

func (*ParsedImport) PackageName added in v1.0.3

func (pi *ParsedImport) PackageName() string

type ParsedIndex added in v1.1.0

type ParsedIndex struct {
	Type    interface{}
	Indexes []interface{}
}

type ParsedInterface

type ParsedInterface struct {
	Methods    []ParsedInterfaceMethod
	Incomplete bool
}

type ParsedInterfaceMethod added in v1.0.1

type ParsedInterfaceMethod = ParsedField

type ParsedMap

type ParsedMap struct {
	KeyType   interface{}
	ValueType interface{}
}

type ParsedNativeType

type ParsedNativeType struct {
	Name string
}

type ParsedNonNativeType

type ParsedNonNativeType struct {
	Name string
	Ref  *ParsedDeclaration
}

type ParsedPointer

type ParsedPointer struct {
	ToType interface{}
}

type ParsedStruct

type ParsedStruct struct {
	Fields []ParsedStructField
}

type ParsedStructField

type ParsedStructField = ParsedField

type ParsedTag

type ParsedTag string

ParsedTag is a string like the original "reflect" library. But this extension allows to split the tag into a comma separated list of properties in the format key=value.

func (ParsedTag) GetBoolProperty

func (pt ParsedTag) GetBoolProperty(key string) bool

func (ParsedTag) GetProperty

func (pt ParsedTag) GetProperty(key string) (string, bool)

func (ParsedTag) HasProperty

func (pt ParsedTag) HasProperty(key string) bool

type ParsedTags

type ParsedTags map[string]ParsedTag

func (*ParsedTags) GetTag

func (pts *ParsedTags) GetTag(tag string) (ParsedTag, bool)

func (*ParsedTags) HasTag

func (pts *ParsedTags) HasTag(tag string) bool

Jump to

Keyboard shortcuts

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