underlying

package module
v0.0.0-...-37bc1f6 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2018 License: MIT Imports: 1 Imported by: 0

README

Underlying

godoc.org

Convert to a type which contains only basic types.

Documentation

Overview

Package underlying implements a converts to a type which contains only basic types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(t types.Type, ptrElem bool) types.Type

Convert converts specified type to a type which contains only basic types. e.g. (MyInt is defined by "type MyInt int")

[]MyInt -> []int
<-chan MyInt -> <-chan int
map[MyInt]MyInt -> map[int]int

if ptrElem is true, Convert converts a pointer type to its elem type. e.g. *string -> string

Example
package main

import (
	"fmt"
	"go/ast"
	"go/importer"
	"go/parser"
	"go/token"
	"go/types"

	"github.com/tenntenn/underlying"
)

func main() {
	const src = `package main
	type MyInt int
	type Example struct {
		N MyInt
		S *string
	}
	func main() {}`

	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "main.go", src, 0)
	if err != nil {
		panic(err)
	}

	info := &types.Info{
		Defs: map[*ast.Ident]types.Object{},
	}

	config := &types.Config{
		Importer: importer.Default(),
	}

	pkg, err := config.Check("main", fset, []*ast.File{f}, info)
	if err != nil {
		panic(err)
	}

	typ := pkg.Scope().Lookup("Example").Type()
	fmt.Println(underlying.Convert(typ, true))

}
Output:

struct{N int; S string}

Types

type Converter

type Converter struct {
	PtrElem bool
}

Converter converts specified type to a type which contains only basic types.

func (*Converter) Convert

func (c *Converter) Convert(t types.Type) types.Type

Convert converts specified type to a type which contains only basic types. e.g. (MyInt is defined by "type MyInt int")

[]MyInt -> []int
<-chan MyInt -> <-chan int
map[MyInt]MyInt -> map[int]int

if ptrElem is true, Convert converts a pointer type to its elem type. e.g. *string -> string

Jump to

Keyboard shortcuts

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