Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
feat: add the ability to load Lua patch before schema files
Browse files Browse the repository at this point in the history
Allows the user of a LuaValidator to load Lua code to patch the
environment before loading the schema files.

#371
  • Loading branch information
javierguerragiraldez authored Aug 18, 2022
1 parent ba57a72 commit 4ad98e8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/jeremywohl/flatten v1.0.1
github.com/kong/go-kong v0.30.0
github.com/kong/go-wrpc v0.0.0-20220713182016-0ac0ed43d830
github.com/kong/goks v0.4.9
github.com/kong/goks v0.4.10
github.com/mattn/go-sqlite3 v1.14.14
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus/client_golang v1.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ github.com/kong/go-kong v0.30.0 h1:iD13//BnR5rkhJWsl1bA/X3n2dKDBYjXb14M6CYshcA=
github.com/kong/go-kong v0.30.0/go.mod h1:DREJ1lzBSOV8d7VHhMweTCkZrv4eyx3YgbXQcdOxBvk=
github.com/kong/go-wrpc v0.0.0-20220713182016-0ac0ed43d830 h1:KKUStSi53GgOWWnIqQx2RIYcORQX75z1t0jWTR8n4NA=
github.com/kong/go-wrpc v0.0.0-20220713182016-0ac0ed43d830/go.mod h1:y0xoS99VcKjQ7Q0p8y3ppSLIeKOF7apHLv3TdpThqnY=
github.com/kong/goks v0.4.9 h1:Nm8hf7yzYjpcIMCxMwT7+z8UlGG7up34QSnngyuK4zU=
github.com/kong/goks v0.4.9/go.mod h1:nyLemg1ZZDo9k9iloaP4ht1OECS9EoNpqUwUwTfsWTg=
github.com/kong/goks v0.4.10 h1:mt5LbP3w1dxtgER37n3SbRYOtw89WtgsgMeqRD3cLII=
github.com/kong/goks v0.4.10/go.mod h1:3bpw439i+AlBeRiDcSgaltu2KM/rFgMkr4tggFUbEYE=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
9 changes: 9 additions & 0 deletions internal/plugin/validators/lua_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ func (v *LuaValidator) ProcessDefaults(ctx context.Context, plugin *grpcModel.Pl
return nil
}

func (v *LuaValidator) LoadPatch(pgkName string) error {
_, err := v.goksV.Execute("require(...)", pgkName)
if err != nil {
return fmt.Errorf("failed to load %s: %w", pgkName, err)
}

return nil
}

func (v *LuaValidator) LoadSchemasFromEmbed(fs embed.FS, dirName string) error {
dirEntries, err := fs.ReadDir(dirName)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions internal/plugin/validators/lua_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ func TestNewLuaValidator(t *testing.T) {
})
}

func TestLuaValidator_LoadPatch(t *testing.T) {
validator, err := NewLuaValidator(Opts{
Logger: log.Logger,
InjectFS: &testdata.LuaTree,
})
require.NoError(t, err)

// let's use a "standard" module
v, err := validator.goksV.Execute(`return require "version"`)
require.NoError(t, err)
require.Equal(t, "0.0.1", v)

// now load a patch
err = validator.LoadPatch("bump_version")
require.NoError(t, err)

// and verify that any new use gets the patched content
v, err = validator.goksV.Execute(`return require "version"`)
require.NoError(t, err)
require.Equal(t, "0.1-extra-plus", v)
}

func TestLoadSchemasFromEmbed(t *testing.T) {
validator, err := NewLuaValidator(Opts{Logger: log.Logger})
require.Nil(t, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "version"

package.loaded["version"] = "0.1-extra-plus"

0 comments on commit 4ad98e8

Please sign in to comment.