Govy library is released along with a binary which comes as a sidekick.
Currently it has a single subcommand, inferpath.
Usage of govy:
govy <subcommand> [flags]
Subcommands:
inferpath
In order to install govy binary, run:
go install github.com/nobl9/govy/cmd/govy@latestAlternatively you can use tools.go file,
as described in Go wiki.
//go:build tools
package main
import (
_ "github.com/nobl9/govy/cmd/govy"
)inferpath is a code generator that can be used to infer relative property paths
for govy getter expressions.
It searches for invocations of govy.PropertyRules constructors,
like govy.For, and follows the AST to find the path returned
from govy.PropertyGetter.
It produces a single file with code registering all inferred paths.
It is important to make sure the generated file is loaded BEFORE any
govy.PropertyRules constructors are called.
Otherwise the inferred paths will not be available to the govy package.
The generated paths are relative fragments such as name or students[0].index.
They do not include a leading $ root segment.
To generate a file with inferred paths, run the following command from the root of your project:
govy inferpath -dir <directory> -pkg <package> -filename <filename>The following flags are available:
Usage of govy inferpath:
-dir string
directory path to save the generated file
-filename string
generated file name (default "govy_inferred_paths.go")
-pkg string
package name of the generated file
// Code generated by "govy inferpath -dir . -pkg main"; DO NOT EDIT.
package main
import (
"github.com/nobl9/govy/pkg/govyconfig"
"github.com/nobl9/govy/pkg/jsonpath"
)
func init() {
inferredPaths := map[string]govyconfig.InferredPath{
"student/nested.go:13": {
Path: jsonpath.Parse("name"),
File: "student/nested.go",
Line: 13,
},
"university/university.go:13": {
Path: jsonpath.Parse("name"),
File: "university/university.go",
Line: 13,
},
"validation.go:11": {
Path: jsonpath.Parse("name"),
File: "validation.go",
Line: 11,
},
"validation.go:13": {
Path: jsonpath.Parse("university.name"),
File: "validation.go",
Line: 13,
},
"validation.go:15": {
Path: jsonpath.Parse("university"),
File: "validation.go",
Line: 15,
},
}
for _, path := range inferredPaths {
govyconfig.SetInferredPath(path)
}
}