Skip to content

Commit

Permalink
Merge pull request #11 from creker/goembed
Browse files Browse the repository at this point in the history
Replace markbates/pkger with native Go embed
  • Loading branch information
Andrey Kuzmin committed Feb 7, 2022
2 parents 9875ea7 + f5d19dd commit a051f47
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 131 deletions.
32 changes: 2 additions & 30 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,14 @@ on:
branches: [master]

jobs:
pkger:
name: pkger
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.14
uses: actions/setup-go@v1
with:
go-version: 1.14
id: go

- name: Install pkger
run: go get github.com/markbates/pkger/cmd/pkger

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: check generated files
run: |
export PATH=$PATH:$(go env GOPATH)/bin
sum_before=$(go run ./cmd/nan/ checksum)
go generate
sum_after=$(go run ./cmd/nan/ checksum)
echo "$sum_before"
echo "$sum_after"
if [ "$sum_before" != "$sum_after" ] ; then
exit 2
fi
coverage:
name: Coverage
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.14
- name: Set up Go 1.16
uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: 1.16
id: go

- name: Check out code into the Go module directory
Expand Down
29 changes: 10 additions & 19 deletions cmd/nan/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sort"
"strings"

"github.com/markbates/pkger"
"github.com/kak-tus/nan"
)

const (
Expand Down Expand Up @@ -65,22 +65,13 @@ func generateExtra() {
_, *pkgName = filepath.Split(wd)
}

var templateFilename string

templateFilename = pkger.Include("/nan_template.go")
dataNan := readTemplate(templateFilename)
templateFilename = pkger.Include("/json_template.go")
dataJSON := readTemplate(templateFilename)
templateFilename = pkger.Include("/json_template.go")
dataGoccyJSON := convertJsonToGoccyJson(readTemplate(templateFilename), false)
templateFilename = pkger.Include("/jsoniter_template.go")
dataJsoniter := readTemplate(templateFilename)
templateFilename = pkger.Include("/easyjson_template.go")
dataEasyjson := readTemplate(templateFilename)
templateFilename = pkger.Include("/sql_template.go")
dataSQL := readTemplate(templateFilename)
templateFilename = pkger.Include("/cql_template.go")
dataCQL := readTemplate(templateFilename)
dataNan := readEmbeddedTemplate("nan_template.go")
dataJSON := readEmbeddedTemplate("json_template.go")
dataGoccyJSON := convertJsonToGoccyJson(readEmbeddedTemplate("json_template.go"), false)
dataJsoniter := readEmbeddedTemplate("jsoniter_template.go")
dataEasyjson := readEmbeddedTemplate("easyjson_template.go")
dataSQL := readEmbeddedTemplate("sql_template.go")
dataCQL := readEmbeddedTemplate("cql_template.go")

files := flag.Args()

Expand Down Expand Up @@ -304,8 +295,8 @@ func (v *visitor) Visit(node ast.Node) (w ast.Visitor) {
return nil
}

func readTemplate(name string) []byte {
templateFile, err := pkger.Open(name)
func readEmbeddedTemplate(name string) []byte {
templateFile, err := nan.EmbeddedSources.Open(name)
if err != nil {
panic(err)
}
Expand Down
44 changes: 22 additions & 22 deletions cmd/nan/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/markbates/pkger"
"github.com/kak-tus/nan"
)

func generateDefault() {
Expand Down Expand Up @@ -41,49 +41,49 @@ func generateDefault() {

files := make([]string, 0)
if *cql {
files = append(files, pkger.Include("/cql.go"))
files = append(files, pkger.Include("/cql_helpers.go"))
files = append(files, "cql.go")
files = append(files, "cql_helpers.go")
}

if *easyjson {
files = append(files, pkger.Include("/easyjson.go"))
files = append(files, "easyjson.go")
}

if *json {
files = append(files, pkger.Include("/json.go"))
files = append(files, "json.go")
}

if *jsoniter {
files = append(files, pkger.Include("/jsoniter.go"))
files = append(files, "jsoniter.go")
}

if *goccyjson {
files = append(files, pkger.Include("/json.go"))
files = append(files, "json.go")
}

if *sql {
files = append(files, pkger.Include("/sql.go"))
files = append(files, pkger.Include("/sql_convert.go"))
files = append(files, "sql.go")
files = append(files, "sql_convert.go")
}

if *text {
files = append(files, pkger.Include("/text.go"))
files = append(files, "text.go")
}

// We have files to generate (user pass some options), so add other files
if len(files) != 0 {
files = append(
files,
pkger.Include("/nan.go"),
pkger.Include("/helpers.go"),
pkger.Include("/LICENSE"),
)
if len(files) == 0 {
flag.PrintDefaults()
return
}

for i := range files {
_, filename := filepath.Split(files[i])
files = append(
files,
"nan.go",
"helpers.go",
"LICENSE",
)

in, err := pkger.Open(files[i])
for i := range files {
in, err := nan.EmbeddedSources.Open(files[i])
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func generateDefault() {
}
}

if err := ioutil.WriteFile(filename, src, 0644); err != nil {
if err := ioutil.WriteFile(files[i], src, 0644); err != nil {
panic(err)
}

Expand Down
40 changes: 0 additions & 40 deletions cmd/nan/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package main

import (
"crypto/md5"
"fmt"
"io"
"os"
"runtime/debug"

"github.com/markbates/pkger"
)

const genStr = "// Code generated by \"nan %s\"; DO NOT EDIT.\n\n"
Expand All @@ -25,8 +21,6 @@ func main() {
generateDefault()
case "extra":
generateExtra()
case "checksum":
generateChecksum()
case "version":
printVersion()
default:
Expand All @@ -37,40 +31,6 @@ nan extra - generate marshalers implementations for extra types, defined by user
}
}

func generateChecksum() {
files := []string{
pkger.Include("/nan.go"),
pkger.Include("/helpers.go"),
pkger.Include("/LICENSE"),
pkger.Include("/cql.go"),
pkger.Include("/cql_helpers.go"),
pkger.Include("/easyjson.go"),
pkger.Include("/json.go"),
pkger.Include("/jsoniter.go"),
pkger.Include("/sql.go"),
pkger.Include("/sql_convert.go"),
pkger.Include("/text.go"),
}

sum := md5.New()

for i := range files {
in, err := pkger.Open(files[i])
if err != nil {
panic(err)
}

_, err = io.Copy(sum, in)
if err != nil {
panic(err)
}

in.Close()
}

fmt.Printf("%x\n", sum.Sum(nil))
}

func printVersion() {
info, ok := debug.ReadBuildInfo()
if !ok || info == nil {
Expand Down
12 changes: 0 additions & 12 deletions cmd/nan/pkged.go

This file was deleted.

6 changes: 6 additions & 0 deletions embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nan

import "embed"

//go:embed *.go LICENSE
var EmbeddedSources embed.FS
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/kak-tus/nan

go 1.14
go 1.16

require (
github.com/gocql/gocql v0.0.0-20200624222514-34081eda590e
github.com/json-iterator/go v1.1.9
github.com/mailru/easyjson v0.7.1
github.com/markbates/pkger v0.17.0
github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gocql/gocql v0.0.0-20200624222514-34081eda590e h1:SroDcndcOU9BVAduPf/PXihXoR2ZYTQYLXbupbqxAyQ=
github.com/gocql/gocql v0.0.0-20200624222514-34081eda590e/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk=
Expand All @@ -23,8 +21,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.7.1 h1:mdxE1MF9o53iCb2Ghj1VfWvh7ZOwHpnVG/xwXrV90U8=
github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/markbates/pkger v0.17.0 h1:RFfyBPufP2V6cddUyyEVSHBpaAnM1WzaMNyqomeT+iY=
github.com/markbates/pkger v0.17.0/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
Expand Down
2 changes: 0 additions & 2 deletions nan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package nan

import "time"

//go:generate pkger -o cmd/nan

// Validator is implemented by all nan types and returns Valid field
type Validator interface {
IsValid() bool
Expand Down
1 change: 1 addition & 0 deletions nan_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type nullTemplateType struct {
Valid bool // Valid is true if Value is not NULL
}

//nolint:deadcode,unused
func naninitialTemplateType(v initialTemplateType) nullTemplateType {
return nullTemplateType{NullTemplateValue: v, Valid: true}
}
Expand Down
2 changes: 2 additions & 0 deletions sql_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var errNilPtr = errors.New("destination pointer is nil")

//nolint:errorlint
func convertAssign(dest, src interface{}) error {
// Common cases, without reflect.
switch s := src.(type) {
Expand Down Expand Up @@ -342,6 +343,7 @@ func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
return
}

//nolint:errorlint
func strconvErr(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
Expand Down

0 comments on commit a051f47

Please sign in to comment.