goparse

package module
v0.0.0-...-515451b Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2021 License: MIT Imports: 7 Imported by: 0

README

goParse

Several functions for simplify go source parsing

Example

package main

import (
	"bytes"
	"encoding/json"
	"os"

	"github.com/kanocz/goparse"
)

type reqProfile struct {
	ID     int64  `rq:"id,omitempty",json:"id"`
	Name   string `rq:"name",json:"-"`
	Passwd string
}

type otherProfile struct {
	ID   int64  `req:"id"`
	Name string `req:"name"`
}

func main() {
	s, _ := goparse.GetFileStructs("example.go", "req", "rq")
	d, _ := json.Marshal(s)
	var out bytes.Buffer
	json.Indent(&out, d, "", "  ")
	out.WriteTo(os.Stdout)
}

will produce

[
  {
    "Name": "reqProfile",
    "Field": [
      {
        "Name": "ID",
        "Type": "int64",
        "Tags": [
          "id",
          "omitempty"
        ]
      },
      {
        "Name": "Name",
        "Type": "string",
        "Tags": [
          "name"
        ]
      },
      {
        "Name": "Passwd",
        "Type": "string",
        "Tags": null
      }
    ]
  }
]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StructDesc

type StructDesc struct {
	Name  string
	Field []StructField
}

StructDesc contains description of parsed struct

func GetFileStructs

func GetFileStructs(filename string, prefix string, tag string) ([]StructDesc, error)

GetFileStructs returns structs descriptions from parsed go file

type StructField

type StructField struct {
	Name      string
	Type      string
	Tags      []string
	TagParams map[string]string
	TagGt     map[string]int
	TagLt     map[string]int
}

StructField describes field itself

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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