stst

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: MIT Imports: 6 Imported by: 0

README

stst

License ActionsCI Go Report Card

The stst is package to get information defined as type in go package by static analysis.

How to Use

Install

go install github.com/maru44/stst@latest
  1. Load package
  2. Create stst.Parser by stst.NewParser with loaded package
  3. Execute Parse or ParseFile method of created stst.Parser

sample code:

package main

import (
	"fmt"

	"github.com/k0kubun/pp"
	"github.com/maru44/stst"
	"golang.org/x/tools/go/packages"
)

func main() {
	ps, _ := loadPackages("github.com/maru44/stst/tests/data/aaa")

	var schemas []*stst.Schema
	for _, pk := range ps {
		p := stst.NewParser(pk)
		s := p.Parse()
		schemas = append(schemas, s...)
	}
	pp.Println(schemas)
}

func loadPackages(ps ...string) ([]*packages.Package, error) {
	cfg := &packages.Config{
		Mode: packages.NeedName | packages.NeedFiles | packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo,
	}
	pkgs, err := packages.Load(cfg, ps...)
	if err != nil {
		return nil, fmt.Errorf("failed to load package: %w", err)
	}
	return pkgs, err
}

result:

[]*stst.Schema{
  &stst.Schema{
    Name:   "Intf",
    Fields: []*stst.Field{
      &stst.Field{
        Name:                "Hello",
        Func:                &stst.Func{
          Args:    []*stst.Field{},
          Results: []*stst.Field{
            &stst.Field{
              Name: "string",
              Type: &stst.Type{
                Underlying: "string",
                TypeName:   "string",
              },
            },
          },
        },
      },
    },
    Type: &stst.Type{
      Underlying: "github.com/maru44/stst/tests/data/aaa.Intf",
      PkgID:    "github.com/maru44/stst/tests/data/aaa",
      PkgPlusName: "aaa.Intf",
      TypeName:   "Intf",
    },
    IsInterface:  true,
  },
  &stst.Schema{
    Name:   "IntSample",
    Type:   &stst.Type{
      Underlying: "int",
      TypeName:   "int",
    },
  },
  &stst.Schema{
    Name:   "Sample",
    Fields: []*stst.Field{
      &stst.Field{
        Name: "Str",
        Type: &stst.Type{
          Underlying: "string",
          TypeName:   "string",
        },
        Tags:                []*stst.Tag{
          &stst.Tag{
            Key:    "tag0",
            Values: []string{
              "xxx",
            },
            RawValue: "xxx",
          },
          &stst.Tag{
            Key:    "tag1",
            Values: []string{
              "yyy",
              "zzz",
            },
            RawValue: "yyy,zzz",
          },
        },
        Comment: []string{
          "// comment",
        },
      },
    },
    Type: &stst.Type{
      Underlying: "github.com/maru44/stst/tests/data/aaa.Sample",
      PkgID:    "github.com/maru44/stst/tests/data/aaa",
      PkgPlusName: "aaa.Sample",
      TypeName:   "Sample",
    },
  },
  &stst.Schema{
    Name:   "prefixes",
    Type:   &stst.Type{
      Underlying: "github.com/maru44/stst/tests/data/aaa.prefixes",
      PkgID:    "github.com/maru44/stst/tests/data/aaa",
      PkgPlusName: "aaa.prefixes",
      TypeName:   "prefixes",
    },
    TypePrefixes: []stst.TypePrefix{
      "[]",
      "*",
      "[]",
      "[]",
      "[]",
      "*",
    },
  },
}

Documentation

Index

Constants

View Source
const (
	TypePrefixPtr   = TypePrefix("*")
	TypePrefixSlice = TypePrefix("[]")

	TypePrefixKindPtr     = TypePrefixKind("pointer")
	TypePrefixKindSlice   = TypePrefixKind("slice")
	TypePrefixKindArray   = TypePrefixKind("array")
	TypePrefixKindUnknown = TypePrefixKind("unknown")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name                string
	Type                *Type
	IsUntitledStruct    bool
	IsUntitledInterface bool
	Tags                []*Tag
	Comment             []string
	Func                *Func
	Map                 *Map
	TypePrefixes        []TypePrefix
	// Schema is only for untitled struct or untitled interface
	Schema *Schema
}

func (*Field) IsFunc

func (f *Field) IsFunc() bool

IsFunc returns whether the Field is function or not.

func (*Field) IsMap

func (f *Field) IsMap() bool

IsFunc returns whether the Field is map or not.

type Func

type Func struct {
	Args    []*Field
	Results []*Field
}

Func has information of args and results

type Map

type Map struct {
	Key   *Field
	Value *Field
}

type Parser

type Parser struct {
	Pkg *packages.Package
}

func NewParser

func NewParser(pkg *packages.Package) *Parser

func (*Parser) Parse

func (p *Parser) Parse() []*Schema

func (*Parser) ParseFile

func (p *Parser) ParseFile(f *ast.File) []*Schema

type Schema

type Schema struct {
	Name         string
	Fields       []*Field
	Type         *Type
	Func         *Func
	Map          *Map
	IsInterface  bool
	TypePrefixes []TypePrefix
	Comment      []string
}

Schema is information for defined as type

func (*Schema) IsFunc

func (s *Schema) IsFunc() bool

IsFunc returns whether the Schema is function or not.

func (*Schema) IsMap

func (s *Schema) IsMap() bool

IsFunc returns whether the Schema is map or not.

type Tag

type Tag struct {
	Key      string
	Values   []string
	RawValue string
}

type Type

type Type struct {
	Underlying UnderlyingType // xxx/yy.ZZZ
	// PkgID is package id.
	PkgID       string // xxx/yy
	PkgPlusName string // yy.ZZZ
	TypeName    string // ZZZ
}

Type is type information.

func (*Type) SetPackage

func (t *Type) SetPackage()

SetPackage is method to set PkgID and PkgPlusName by UnderlyingType.

type TypePrefix

type TypePrefix string

func (TypePrefix) ArrayLength

func (t TypePrefix) ArrayLength() (int, bool)

func (TypePrefix) Kind

func (t TypePrefix) Kind() TypePrefixKind

type TypePrefixKind

type TypePrefixKind string

type UnderlyingType

type UnderlyingType string

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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