A Go preprocessor for package scoped reflection

Related tags

Utilities pkgreflect
Overview

pkgreflect - A Go preprocessor for package scoped reflection

Problem: Go reflection does not support enumerating types, variables and functions of packages.

pkgreflect generates a file named pkgreflect.go in every parsed package directory. This file contains the following maps of exported names to reflection types/values:

var Types = map[string]reflect.Type{ ... }
var Functions = map[string]reflect.Value{ ... }
var Variables = map[string]reflect.Value{ ... }
var Consts = map[string]reflect.Value{ ... }

Command line usage:

pkgreflect --help
pkgreflect [-notypes][-nofuncs][-novars][-noconsts][-unexported][-norecurs][-gofile=filename.go] [DIR_NAME]

If -norecurs is not set, then pkgreflect traverses recursively into sub-directories. If no DIR_NAME is given, then the current directory is used as root.

Comments
  • Works great from command-line, but not (exit code 2) from exec.Command()?

    Works great from command-line, but not (exit code 2) from exec.Command()?

    Great tool, just what I need!

    Here's my issue. So when I issue this following right inside Windows Command Line:

    pkgreflect -novars=true -norecurs=true -nofuncs=true -gofile="_pkgreflect.go" C:\gd\src\github.com\go3d\go-ngine\assets
    

    Then the tool works perfectly fine and creates _pkgreflect.go (I like having auto-generated code files prefixed in some way).

    But I want to call this from my little "go-buildrun" mini build tool. When I issue the exact same command-line to exec.Command("pkgreflect", args...).CombinedOutput() here's my result:

    2012/12/10 10:26:17 RUN: pkgreflect -novars=true -norecurs=true -nofuncs=true -gofile="_pkgreflect.go" C:\gd\src\github.com\go3d\go-ngine\assets
    panic: exit status 2
    
    goroutine 1 [running]:
    main.main()
        C:/gd/src/github.com/metaleap/go-buildrun/main.go:138 +0xb09
    
    goroutine 2 [syscall]:
    created by runtime.main
        C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist767862039/go/src/pkg/runtime/proc.c:221
    
    goroutine 4 [timer goroutine (idle)]:
    created by addtimer
        C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist767862039/go/src/pkg/runtime/ztime_amd64.c:72
    [Finished in 0.1s with exit code 2]
    

    Also the _pkgreflect.go file isn't written. Any idea what's going on?

    Edit: well, essentially the entire stack-trace above is useless in this case, as I'm just panic-ing here when a exec.Command().CombinedOutput() returns an err, in this case it's an "exit status 2" err...

    opened by metaleap 3
  • Rewrite pkgreflect.go only if changed

    Rewrite pkgreflect.go only if changed

    So I use go install for building my packages. It only recompiles&relinks if source files are changed. Using my mini-build-tool "go-buildrun" I can chain other commands prior to issuing the "go install" command itself. In this case, I run the pkgreflect command. This always rewrites a .go source file in the package (namely, pkgreflect.go), even if in terms of content, there aren't any changes. So the package always gets recompiled&relinked because of this, kind of killing my "fast Go build times".

    Would you be interested in resolving this behaviour? I'd be happy to modify the pkgreflect command myself to not rewrite the pkgreflect.go file if there aren't any changes in terms of content -- but only if you would be willing to review that modification and if OK for you, merge it into your repo...

    opened by metaleap 2
  • An option to just use the function itself as the value, not a reflected value

    An option to just use the function itself as the value, not a reflected value

    I'm sure it's nice to have everything reflected, but it limits the accessibility, I forked it and removed the reflecting portion, as that can be done on a per-use basis, while always doing it stops you from having access to the function itself.

    https://github.com/ShayBox/pkgreflect

    opened by ShayBox 1
  • New line added after the first generated line

    New line added after the first generated line

    The pull request adds a new line after the first generated line. The result of this is that the text Code generated by github.com/ungerik/pkgreflect DO NOT EDIT. is not inserted into godoc.

    Without the new line the generated comment is considered to be package level documentation and it is shown in godoc as well. Moreover, if other file names in the package are alphabetically after pkgreflect.go, the generated comment becomes the first sentence in the godoc which I found a bit unfortunate. If this is intended behavior for some use cases, I can make my solution configurable.

    opened by lukasmacko 0
  • Modify comment on the first line in order to skip golint validation on generated files

    Modify comment on the first line in order to skip golint validation on generated files

    according to the https://github.com/golang/go/issues/13560#issuecomment-288457920 the newest golint skips the genereatad file that contains a comment matching regexp mentioned in the comment

    opened by lukasmacko 0
Owner
Erik Unger
CTO & Co-Founder DOMONDA GmbH, Software & Aerospace Enthusiast
Erik Unger
Helpers for making the use of reflection easier

go-xray This is a Golang library with reflection related functions which I use in my different projects. KeyValue This type is used to construct a key

Pieter Claerhout 25 Oct 24, 2022
Faster golang reflection

In order to solve a problem that can work with any golang type, one has no choice but to use reflection. Native golang reflection comes with hefty performance price, on benchmarking simple getter/setter case to manipulate struct dynamically I've seen around 100 time worst performance comparing to statically typed code.

Viant, Inc 25 Dec 21, 2022
Create deep copies (clones) of your maps and slices without using reflection.

DeepCopy DeepCopy helps you create deep copies (clones) of your maps and slices. Create deep copies (clones) of your objects The package is based on t

null 8 Nov 20, 2022
A package for running subprocesses in Go, similar to Python's subprocesses package.

A package for running subprocesses in Go, similar to Python's subprocesses package.

Esteban Garcia 5 Jul 28, 2022
Utility to restrict which package is allowed to import another package.

go-import-rules Utility to restrict which package is allowed to import another package. This tool will read import-rules.yaml or import-rules.yml in t

PAYFAZZ 0 Jan 7, 2022
Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English 简体中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

null 52 Dec 9, 2022
reactssr is a package for rendering React applications.

reactssr A Go package to perform Server Side Rendering of React apps. Example usage Given a bundle produced from an additional entrypoint to your appl

Travis Cline 94 Jan 9, 2023
Package ethtool allows control of the Linux ethtool generic netlink interface.

ethtool Package ethtool allows control of the Linux ethtool generic netlink interface.

Matt Layher 47 Dec 14, 2022
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Maxime Soulé 337 Jan 5, 2023
A well tested and comprehensive Golang statistics library package with no dependencies.

Stats - Golang Statistics Package A well tested and comprehensive Golang statistics library / package / module with no dependencies. If you have any s

Montana Flynn 2.6k Dec 30, 2022
A computational topology package for gophers.

Simplices; simplicial complexes; simplicial chains; chain, cycle, boundary and homology groups; sets of simplices; methods for computing boundaries, Euler characteristics, Euler integrals, and Betti numbers, and more (with even more to come)!

Raphael Reyna 10 Apr 19, 2021
Go package implementing Bloom filters

Go package implementing Bloom filters

Will Fitzgerald 1.8k Dec 30, 2022
go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Nick Snyder 2.3k Jan 2, 2023
Go package to generate and manage color palettes & schemes 🎨

Go package to generate and manage color palettes & schemes

Christian Muehlhaeuser 503 Dec 29, 2022
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Spiral Scout 1.1k Dec 28, 2022
Gene parsing package for Axie Infinity

agp Package agp is a gene parsing package for Axie Infinity. The name agp stands for "Axie Gene Parser" which decodes the hex representation of an Axi

Shane Maglangit 26 Apr 18, 2022
keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

Gunosy 5 Apr 18, 2022
A lightweight casting package for Go projects

Cast GoLobby Cast is a lightweight casting package for Go projects. Documentation Required Go Versions It requires Go v1.11 or newer versions. Install

GoLobby 14 Dec 21, 2022
📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go import "golang.design/x/clipboard" Features Cross platform supports

The golang.design Initiative 315 Dec 24, 2022