diff --git a/Makefile b/Makefile index 427c26e..eb8f74a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,3 @@ -.PHONY:easyjson -easyjson: - easyjson -all ./internal/model/config.go - .PHONY:build build: go build . diff --git a/go.mod b/go.mod index 1bd9ed4..4e148ea 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/sanposhiho/gomockhandler go 1.16 require ( - github.com/mailru/easyjson v0.7.7 golang.org/x/mod v0.4.1 golang.org/x/sync v0.0.0-20190423024810-112230192c58 ) diff --git a/go.sum b/go.sum index 8e073dc..de71f05 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= -github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY= diff --git a/internal/command/generate_config.go b/internal/command/generate_config.go index 393c079..362b89e 100644 --- a/internal/command/generate_config.go +++ b/internal/command/generate_config.go @@ -44,7 +44,7 @@ func (r Runner) GenerateConfig() { destinationPathInPro := util.PathInProject(configDir, originalPath+"/"+r.Args.Destination) r.MockgenRunner.SetDestination(destinationPathInPro) - var sourceChecksum [16]byte + var sourceChecksum string if r.Args.Source != "" { // change source as seen from the config directory. sourcePathInPro := util.PathInProject(configDir, originalPath+"/"+r.Args.Source) diff --git a/internal/command/mockgen.go b/internal/command/mockgen.go index ae6a755..ab2b532 100644 --- a/internal/command/mockgen.go +++ b/internal/command/mockgen.go @@ -35,7 +35,7 @@ func (r Runner) Mockgen() { m := m g.Go(func() error { var runner mockgen.Runner - var sourceChecksum [16]byte + var sourceChecksum string switch m.Mode { case model.ReflectMode: runner = m.ReflectModeRunner diff --git a/internal/mockgen/runner.go b/internal/mockgen/runner.go index 39fa414..936e49c 100644 --- a/internal/mockgen/runner.go +++ b/internal/mockgen/runner.go @@ -16,7 +16,7 @@ type Runner interface { GetDestination() string } -func Checksum(r Runner) ([16]byte, error) { +func Checksum(r Runner) (string, error) { d := r.GetDestination() tmpFile := util.TmpFilePath(d) defer os.Remove(tmpFile) @@ -26,21 +26,21 @@ func Checksum(r Runner) ([16]byte, error) { defer r.SetDestination(d) if err := r.Run(); err != nil { - return [16]byte{}, fmt.Errorf("failed to run mockgen: %v \nPlease run `%s` and check if mockgen works correctly with your options", err, r) + return "", fmt.Errorf("failed to run mockgen: %v \nPlease run `%s` and check if mockgen works correctly with your options", err, r) } checksum, err := util.CalculateCheckSum(tmpFile) if err != nil { - return [16]byte{}, fmt.Errorf("calculate checksum of the mock: %v", err) + return "", fmt.Errorf("calculate checksum of the mock: %v", err) } return checksum, nil } -func SourceChecksum(r Runner) ([16]byte, error) { +func SourceChecksum(r Runner) (string, error) { checksum, err := util.CalculateCheckSum(r.GetSource()) if err != nil { - return [16]byte{}, fmt.Errorf("calculate checksum of the mock source: %v", err) + return "", fmt.Errorf("calculate checksum of the mock source: %v", err) } return checksum, nil diff --git a/internal/model/config.go b/internal/model/config.go index 6c76588..d8ffd70 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -2,7 +2,6 @@ package model import "errors" -//easyjson:json type Config struct { // key: destination Mocks map[string]*Mock `json:"mocks"` diff --git a/internal/model/config_easyjson.go b/internal/model/config_easyjson.go deleted file mode 100644 index 2f1946d..0000000 --- a/internal/model/config_easyjson.go +++ /dev/null @@ -1,636 +0,0 @@ -// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. - -package model - -import ( - json "encoding/json" - easyjson "github.com/mailru/easyjson" - jlexer "github.com/mailru/easyjson/jlexer" - jwriter "github.com/mailru/easyjson/jwriter" - reflectmode "github.com/sanposhiho/gomockhandler/internal/mockgen/reflectmode" - sourcemode "github.com/sanposhiho/gomockhandler/internal/mockgen/sourcemode" -) - -// suppress unused package warning -var ( - _ *json.RawMessage - _ *jlexer.Lexer - _ *jwriter.Writer - _ easyjson.Marshaler -) - -func easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalModel(in *jlexer.Lexer, out *Config) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "mocks": - if in.IsNull() { - in.Skip() - } else { - in.Delim('{') - out.Mocks = make(map[string]*Mock) - for !in.IsDelim('}') { - key := string(in.String()) - in.WantColon() - var v1 *Mock - if in.IsNull() { - in.Skip() - v1 = nil - } else { - if v1 == nil { - v1 = new(Mock) - } - easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalModel1(in, v1) - } - (out.Mocks)[key] = v1 - in.WantComma() - } - in.Delim('}') - } - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalModel(out *jwriter.Writer, in Config) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"mocks\":" - out.RawString(prefix[1:]) - if in.Mocks == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { - out.RawString(`null`) - } else { - out.RawByte('{') - v2First := true - for v2Name, v2Value := range in.Mocks { - if v2First { - v2First = false - } else { - out.RawByte(',') - } - out.String(string(v2Name)) - out.RawByte(':') - if v2Value == nil { - out.RawString("null") - } else { - easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalModel1(out, *v2Value) - } - } - out.RawByte('}') - } - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Config) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalModel(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Config) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalModel(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Config) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalModel(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Config) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalModel(l, v) -} -func easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalModel1(in *jlexer.Lexer, out *Mock) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "checksum": - if in.IsNull() { - in.Skip() - } else { - copy(out.MockCheckSum[:], in.Bytes()) - } - case "source_checksum": - if in.IsNull() { - in.Skip() - } else { - copy(out.SourceChecksum[:], in.Bytes()) - } - case "mode": - out.Mode = mode(in.String()) - case "reflect_mode_runner": - if in.IsNull() { - in.Skip() - out.ReflectModeRunner = nil - } else { - if out.ReflectModeRunner == nil { - out.ReflectModeRunner = new(reflectmode.Runner) - } - easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalMockgenReflectmode(in, out.ReflectModeRunner) - } - case "source_mode_runner": - if in.IsNull() { - in.Skip() - out.SourceModeRunner = nil - } else { - if out.SourceModeRunner == nil { - out.SourceModeRunner = new(sourcemode.Runner) - } - easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalMockgenSourcemode(in, out.SourceModeRunner) - } - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalModel1(out *jwriter.Writer, in Mock) { - out.RawByte('{') - first := true - _ = first - if true { - const prefix string = ",\"checksum\":" - first = false - out.RawString(prefix[1:]) - out.Base64Bytes(in.MockCheckSum[:]) - } - if true { - const prefix string = ",\"source_checksum\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Base64Bytes(in.SourceChecksum[:]) - } - { - const prefix string = ",\"mode\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Mode)) - } - if in.ReflectModeRunner != nil { - const prefix string = ",\"reflect_mode_runner\":" - out.RawString(prefix) - easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalMockgenReflectmode(out, *in.ReflectModeRunner) - } - if in.SourceModeRunner != nil { - const prefix string = ",\"source_mode_runner\":" - out.RawString(prefix) - easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalMockgenSourcemode(out, *in.SourceModeRunner) - } - out.RawByte('}') -} -func easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalMockgenSourcemode(in *jlexer.Lexer, out *sourcemode.Runner) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "source": - out.Source = string(in.String()) - case "destination": - out.Destination = string(in.String()) - case "package": - out.Package = string(in.String()) - case "imports": - out.Imports = string(in.String()) - case "aux_files": - out.AuxFiles = string(in.String()) - case "mock_names": - out.MockNames = string(in.String()) - case "self_package": - out.SelfPackage = string(in.String()) - case "copyright_file": - out.CopyrightFile = string(in.String()) - case "write_pkg_comment": - if in.IsNull() { - in.Skip() - out.WritePkgComment = nil - } else { - if out.WritePkgComment == nil { - out.WritePkgComment = new(bool) - } - *out.WritePkgComment = bool(in.Bool()) - } - case "debug_parser": - if in.IsNull() { - in.Skip() - out.DebugParser = nil - } else { - if out.DebugParser == nil { - out.DebugParser = new(bool) - } - *out.DebugParser = bool(in.Bool()) - } - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalMockgenSourcemode(out *jwriter.Writer, in sourcemode.Runner) { - out.RawByte('{') - first := true - _ = first - if in.Source != "" { - const prefix string = ",\"source\":" - first = false - out.RawString(prefix[1:]) - out.String(string(in.Source)) - } - if in.Destination != "" { - const prefix string = ",\"destination\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Destination)) - } - if in.Package != "" { - const prefix string = ",\"package\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Package)) - } - if in.Imports != "" { - const prefix string = ",\"imports\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Imports)) - } - if in.AuxFiles != "" { - const prefix string = ",\"aux_files\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.AuxFiles)) - } - if in.MockNames != "" { - const prefix string = ",\"mock_names\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.MockNames)) - } - if in.SelfPackage != "" { - const prefix string = ",\"self_package\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.SelfPackage)) - } - if in.CopyrightFile != "" { - const prefix string = ",\"copyright_file\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.CopyrightFile)) - } - if in.WritePkgComment != nil { - const prefix string = ",\"write_pkg_comment\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(*in.WritePkgComment)) - } - if in.DebugParser != nil { - const prefix string = ",\"debug_parser\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(*in.DebugParser)) - } - out.RawByte('}') -} -func easyjson6615c02eDecodeGithubComSanposhihoGomockhandlerInternalMockgenReflectmode(in *jlexer.Lexer, out *reflectmode.Runner) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "package_name": - out.PackageName = string(in.String()) - case "interfaces": - out.Interfaces = string(in.String()) - case "source": - out.Source = string(in.String()) - case "destination": - out.Destination = string(in.String()) - case "package": - out.Package = string(in.String()) - case "imports": - out.Imports = string(in.String()) - case "aux_files": - out.AuxFiles = string(in.String()) - case "build_flags": - out.BuildFlags = string(in.String()) - case "mock_names": - out.MockNames = string(in.String()) - case "self_package": - out.SelfPackage = string(in.String()) - case "copyright_file": - out.CopyrightFile = string(in.String()) - case "exec_only": - out.ExecOnly = string(in.String()) - case "prog_only": - if in.IsNull() { - in.Skip() - out.ProgOnly = nil - } else { - if out.ProgOnly == nil { - out.ProgOnly = new(bool) - } - *out.ProgOnly = bool(in.Bool()) - } - case "write_pkg_comment": - if in.IsNull() { - in.Skip() - out.WritePkgComment = nil - } else { - if out.WritePkgComment == nil { - out.WritePkgComment = new(bool) - } - *out.WritePkgComment = bool(in.Bool()) - } - case "debug_parser": - if in.IsNull() { - in.Skip() - out.DebugParser = nil - } else { - if out.DebugParser == nil { - out.DebugParser = new(bool) - } - *out.DebugParser = bool(in.Bool()) - } - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson6615c02eEncodeGithubComSanposhihoGomockhandlerInternalMockgenReflectmode(out *jwriter.Writer, in reflectmode.Runner) { - out.RawByte('{') - first := true - _ = first - if in.PackageName != "" { - const prefix string = ",\"package_name\":" - first = false - out.RawString(prefix[1:]) - out.String(string(in.PackageName)) - } - if in.Interfaces != "" { - const prefix string = ",\"interfaces\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Interfaces)) - } - if in.Source != "" { - const prefix string = ",\"source\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Source)) - } - if in.Destination != "" { - const prefix string = ",\"destination\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Destination)) - } - if in.Package != "" { - const prefix string = ",\"package\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Package)) - } - if in.Imports != "" { - const prefix string = ",\"imports\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Imports)) - } - if in.AuxFiles != "" { - const prefix string = ",\"aux_files\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.AuxFiles)) - } - if in.BuildFlags != "" { - const prefix string = ",\"build_flags\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.BuildFlags)) - } - if in.MockNames != "" { - const prefix string = ",\"mock_names\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.MockNames)) - } - if in.SelfPackage != "" { - const prefix string = ",\"self_package\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.SelfPackage)) - } - if in.CopyrightFile != "" { - const prefix string = ",\"copyright_file\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.CopyrightFile)) - } - if in.ExecOnly != "" { - const prefix string = ",\"exec_only\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.ExecOnly)) - } - if in.ProgOnly != nil { - const prefix string = ",\"prog_only\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(*in.ProgOnly)) - } - if in.WritePkgComment != nil { - const prefix string = ",\"write_pkg_comment\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(*in.WritePkgComment)) - } - if in.DebugParser != nil { - const prefix string = ",\"debug_parser\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(*in.DebugParser)) - } - out.RawByte('}') -} diff --git a/internal/model/mock.go b/internal/model/mock.go index 4c0a19b..7b16190 100644 --- a/internal/model/mock.go +++ b/internal/model/mock.go @@ -15,14 +15,14 @@ const ( ) type Mock struct { - MockCheckSum [16]byte `json:"checksum,omitempty"` - SourceChecksum [16]byte `json:"source_checksum,omitempty"` + MockCheckSum string `json:"checksum,omitempty"` + SourceChecksum string `json:"source_checksum,omitempty"` Mode mode `json:"mode"` ReflectModeRunner *reflectmode.Runner `json:"reflect_mode_runner,omitempty"` SourceModeRunner *sourcemode.Runner `json:"source_mode_runner,omitempty"` } -func NewMock(mockChecksum, sourceChecksum [16]byte, genrunner mockgen.Runner) Mock { +func NewMock(mockChecksum, sourceChecksum string, genrunner mockgen.Runner) Mock { rrunner, srunner := convertMockgenRunner(genrunner) mode := Unknown if rrunner != nil { diff --git a/internal/repository/config/config.go b/internal/repository/config/config.go index f8d1aea..e1b5c01 100644 --- a/internal/repository/config/config.go +++ b/internal/repository/config/config.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "os" - "github.com/mailru/easyjson" "github.com/sanposhiho/gomockhandler/internal/model" ) @@ -18,9 +17,9 @@ func NewRepository() Repository { } func (r *Repository) Put(m *model.Config, path string) error { - d, err := easyjson.Marshal(m) + d, err := json.Marshal(m) if err != nil { - return fmt.Errorf("easyjson marshal: %w", err) + return fmt.Errorf("json marshal: %w", err) } var buf bytes.Buffer @@ -42,9 +41,9 @@ func (r *Repository) Get(path string) (*model.Config, error) { } var m model.Config - err = easyjson.Unmarshal(raw, &m) + err = json.Unmarshal(raw, &m) if err != nil { - return nil, fmt.Errorf("easyjson unmarshal: %w", err) + return nil, fmt.Errorf("json unmarshal: %w", err) } return &m, err diff --git a/internal/util/random.go b/internal/util/random.go index 5ea02d4..5b23013 100644 --- a/internal/util/random.go +++ b/internal/util/random.go @@ -2,20 +2,22 @@ package util import ( "crypto/md5" + "encoding/base64" "fmt" "io/ioutil" "path/filepath" "strings" ) -func CalculateCheckSum(filePath string) ([16]byte, error) { +func CalculateCheckSum(filePath string) (string, error) { file, err := ioutil.ReadFile(filePath) if err != nil { - return [16]byte{}, fmt.Errorf("failed read file. filename: %s, err: %w", filePath) + return "", fmt.Errorf("failed read file. filename: %s, err: %w", filePath) } hash := md5.Sum(file) - return hash, nil + strhash := base64.StdEncoding.EncodeToString(hash[:]) + return strhash, nil } func PathInProject(projectRoot, path string) string {