Skip to content

Commit 3190c41

Browse files
author
Openset
committed
Add: langSupport
1 parent afe7ca8 commit 3190c41

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

internal/leetcode/question_data.go

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,65 @@ func (question questionType) PackageName() string {
157157
}
158158

159159
func (question questionType) SaveCodeSnippet() {
160+
langSupport := [...]struct {
161+
slug string
162+
handle func(questionType, codeSnippetsType)
163+
}{
164+
{"golang", handleCodeGolang},
165+
{"python3", handleCodePython},
166+
{"python", handleCodePython},
167+
{"bash", handleCodeBash},
168+
{"mysql", handleCodeSQL},
169+
{"mssql", handleCodeSQL},
170+
{"oraclesql", handleCodeSQL},
171+
}
172+
codeSet := make(map[string]codeSnippetsType)
160173
for _, code := range question.CodeSnippets {
161-
if code.LangSlug == "golang" {
162-
filePath := question.getFilePath(question.TitleSnake() + ".go")
163-
var buf bytes.Buffer
164-
buf.WriteString(fmt.Sprintf("package %s\n\n", question.PackageName()))
165-
buf.WriteString(code.Code)
166-
buf.WriteString("\n")
167-
filePutContents(filePath, buf.Bytes())
168-
buf.Reset()
169-
// match function name
170-
reg := regexp.MustCompile(`func (\w+?)\(`)
171-
matches := reg.FindStringSubmatch(code.Code)
172-
funcName := "Func"
173-
if len(matches) >= 2 {
174-
funcName = matches[1]
175-
}
176-
fileTest := question.getFilePath(question.TitleSnake() + "_test.go")
177-
buf.WriteString(strings.NewReplacer(
178-
"{{packageName}}", question.PackageName(),
179-
"{{funcName}}", strings.Title(funcName),
180-
).Replace(testTpl))
181-
filePutContents(fileTest, buf.Bytes())
182-
} else if len(question.CodeSnippets) == 1 {
183-
filePath := question.getFilePath(question.TitleSnake() + "." + code.LangSlug)
184-
filePutContents(filePath, []byte(code.Code))
174+
codeSet[code.LangSlug] = code
175+
}
176+
for _, lang := range langSupport {
177+
if code, ok := codeSet[lang.slug]; ok {
178+
lang.handle(question, code)
179+
break
185180
}
186181
}
187182
}
188183

184+
func (question questionType) saveCodeContent(content, ext string) {
185+
filePath := question.getFilePath(question.TitleSnake() + ext)
186+
filePutContents(filePath, []byte(content))
187+
}
188+
189+
func handleCodeGolang(question questionType, code codeSnippetsType) {
190+
content := fmt.Sprintf("package %s\n\n", question.PackageName())
191+
content += code.Code + "\n"
192+
question.saveCodeContent(content, ".go")
193+
// match function name
194+
reg := regexp.MustCompile(`func (\w+?)\(`)
195+
matches := reg.FindStringSubmatch(code.Code)
196+
funcName := "Func"
197+
if len(matches) >= 2 {
198+
funcName = matches[1]
199+
}
200+
content = strings.NewReplacer(
201+
"{{packageName}}", question.PackageName(),
202+
"{{funcName}}", strings.Title(funcName),
203+
).Replace(testTpl)
204+
question.saveCodeContent(content, "_test.go")
205+
}
206+
207+
func handleCodeBash(question questionType, code codeSnippetsType) {
208+
question.saveCodeContent("#!/usr/bin/env bash\n\n"+code.Code, ".bash")
209+
}
210+
211+
func handleCodePython(question questionType, code codeSnippetsType) {
212+
question.saveCodeContent("#!/usr/bin/env python\n\n"+code.Code, ".py")
213+
}
214+
215+
func handleCodeSQL(question questionType, code codeSnippetsType) {
216+
question.saveCodeContent(code.Code, ".sql")
217+
}
218+
189219
const testTpl = `package {{packageName}}
190220
191221
import "testing"

0 commit comments

Comments
 (0)