Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pre submit code modifiers #318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"strings"

"github.com/charmbracelet/log"
"github.com/dop251/goja"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/j178/leetgo/config"
"github.com/j178/leetgo/lang"
Expand Down Expand Up @@ -86,10 +88,18 @@ func submitSolution(
*leetcode.SubmitCheckResult,
error,
) {
modifiers, err := getPostModifiers(gen)
if err != nil {
return nil, fmt.Errorf("failed to get post modifiers: %w", err)
}

solution, err := lang.GetSolutionCode(q)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we integrate the pre-submit modification directly into the lang.GetSolutionCode function? What do you think?

if err != nil {
return nil, fmt.Errorf("failed to get solution code: %w", err)
}
for _, m := range modifiers {
solution = m(solution)
}

spin := newSpinner(cmd.ErrOrStderr())
spin.Suffix = " Submitting solution..."
Expand All @@ -116,6 +126,49 @@ func submitSolution(
return testResult.(*leetcode.SubmitCheckResult), nil
}

func getPostModifiers(lang lang.Lang) ([]func(string) string, error) {
modifiers := viper.Get("code." + lang.Slug() + ".post-modifiers")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to name it something like pre-submit-modifiers.

if modifiers == nil || len(modifiers.([]any)) == 0 {
modifiers = viper.Get("code." + lang.ShortName() + ".post-modifiers")
}
if modifiers == nil || len(modifiers.([]any)) == 0 {
modifiers = viper.Get("code.post-modifiers")
}
if modifiers == nil {
return nil, nil
}

var funcs []func(string) string
for _, m := range modifiers.([]any) {
m := m.(map[string]any)
name, script := "", ""

if m["script"] != nil {
script = m["script"].(string)
vm := goja.New()
_, err := vm.RunString(script)
if err != nil {
return nil, fmt.Errorf("failed to run script: %w", err)
}
var jsFn func(string) string
if vm.Get("modify") == nil {
return nil, fmt.Errorf("failed to get modify function")
}
err = vm.ExportTo(vm.Get("modify"), &jsFn)
if err != nil {
return nil, fmt.Errorf("failed to export function: %w", err)
}
f := func(s string) string {
return jsFn(s)
}
funcs = append(funcs, f)
continue
}
log.Warn("invalid modifier, ignored", "name", name, "script", script)
}
return funcs, nil
}

func appendToTestCases(q *leetcode.QuestionData, result *leetcode.SubmitCheckResult) (bool, error) {
genResult, err := lang.GeneratePathsOnly(q)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ func runTestRemotely(
*leetcode.RunCheckResult,
error,
) {
modifiers, err := getPostModifiers(gen)
if err != nil {
return nil, fmt.Errorf("failed to get post modifiers: %w", err)
}

solution, err := lang.GetSolutionCode(q)
if err != nil {
return nil, fmt.Errorf("failed to get solution code: %w", err)
}
for _, m := range modifiers {
solution = m(solution)
}
err = q.Fulfill()
if err != nil {
return nil, fmt.Errorf("failed to fetch question: %s", err)
Expand Down
Loading