Skip to content

Commit

Permalink
feat: go
Browse files Browse the repository at this point in the history
  • Loading branch information
bionicles committed Dec 18, 2023
1 parent 57c16aa commit c2e066d
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 32 deletions.
46 changes: 46 additions & 0 deletions tests/more_languages/group3/go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// go_test.go
package main

import (
"context"
"errors"
"fmt"
)

type Greeting struct {
message string
}

func (g Greeting) sayHello() {
fmt.Println(g.message)
}

func createGreeting(m string) Greeting {
return Greeting{message: m}
}

type SomethingLong struct {
// struct fields...
}

// Complex multiline function signature
func (s *SomethingLong) WithAReasonableName(
ctx context.Context,
param1 string,
param2 int,
param3 map[string]interface{},
callback func(int) error,
) (resultType, error) {
// Function implementation...

// Dummy return
return resultType{}, errors.New("not implemented")
}

type resultType struct {
// struct fields...
}

func main() {
// Main function implementation...
}
16 changes: 0 additions & 16 deletions tests/more_languages/group_todo/go_test.go

This file was deleted.

84 changes: 75 additions & 9 deletions tests/test_more_language_units.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# tests/test_more_language_units.py
from typing import Literal
import pytest

from rich import print
Expand Down Expand Up @@ -76,7 +77,19 @@
),
],
)
def test_more_languages_group1(file, expected):
def test_more_languages_group1(
file: Literal[
"tests/more_languages/group1/COBOL_TEST.CBL",
"tests/more_languages/group1/JavaTest.java",
"tests/more_languages/group1/JuliaTest.jl",
"tests/more_languages/group1/KotlinTest.kt",
"tests/more_languages/group1/LispTest.lisp",
"tests/more_languages/group1/LuaTest.lua",
"tests/more_languages/group1/ObjectiveCTest.m",
"tests/more_languages/group1/OcamlTest.ml",
],
expected: list[str],
):
print(f"{file=}")
result = parse_file(file)
print(f"{result=}")
Expand Down Expand Up @@ -158,7 +171,17 @@ def test_more_languages_group1(file, expected):
),
],
)
def test_more_languages_group2(file, expected):
def test_more_languages_group2(
file: Literal[
"tests/more_languages/group2/apl_test.apl",
"tests/more_languages/group2/PerlTest.pl",
"tests/more_languages/group2/PhpTest.php",
"tests/more_languages/group2/ScalaTest.scala",
"tests/more_languages/group2/c_test.c",
"tests/more_languages/group2/PowershellTest.ps1",
],
expected: list[str],
):
print(f"{file=}")
result = parse_file(file)
print(f"{expected=}")
Expand Down Expand Up @@ -194,9 +217,29 @@ def test_more_languages_group2(file, expected):
"void myFunction(string fname, int age)",
],
),
(
"tests/more_languages/group3/go_test.go",
[
"type Greeting struct",
"func (g Greeting) sayHello()",
"func createGreeting(m string) Greeting",
"type SomethingLong struct",
"""func (s *SomethingLong) WithAReasonableName(
ctx context.Context,
param1 string,
param2 int,
param3 map[string]interface{},
callback func(int) error,
) (resultType, error)""",
"type resultType struct",
"func main()",
],
),
],
)
def test_more_languages_group3(file, expected):
def test_more_languages_group3(
file: Literal["tests/more_languages/group3/cpp_test.cpp"], expected: str
):
print(f"{file=}")
result = parse_file(file)
print(f"{result=}")
Expand Down Expand Up @@ -268,7 +311,13 @@ def test_more_languages_group3(file, expected):
),
],
)
def test_more_languages_group4(file, expected):
def test_more_languages_group4(
file: Literal[
"tests/more_languages/group4/rust_test.rs",
"tests/more_languages/group4/tf_test.tf",
],
expected: list[str],
):
print(f"{file=}")
result = parse_file(file)
print(f"{result=}")
Expand Down Expand Up @@ -520,7 +569,27 @@ def test_more_languages_group4(file, expected):
),
],
)
def test_more_languages_group5(file, expected):
def test_more_languages_group5(
file: Literal[
"tests/more_languages/group5/ansible_test.yml",
"tests/more_languages/group5/k8s_test.yaml",
"tests/more_languages/group5/checkbox_test.md",
"tests/more_languages/group5/checkbox_test.txt",
".github/workflows/unix.yml",
"tests/more_languages/group5/sql_test.sql",
"tests/more_languages/group5/app.component.spec.ts",
"tests/more_languages/group5/app.component.ts",
"tests/more_languages/group5/app-routing.module.ts",
"tests/more_languages/group5/app.module.ts",
"tests/more_languages/group5/requirements_test.txt",
"tests/more_languages/group5/testJsonSchema.json",
"tests/more_languages/group5/Makefile",
"tests/more_languages/group5/test.env",
"tests/more_languages/group5/testPackage.json",
"tests/more_languages/group5/environment.test.ts",
],
expected: list[str],
):
print(f"{file=}")
result = parse_file(file)
print(f"{result=}")
Expand Down Expand Up @@ -590,10 +659,7 @@ def test_more_languages_group5(file, expected):
# "tests/more_languages/group4/fsharp_test.fs",
# ["type Person -> member this.SayHello", "let person"],
# ),
# (
# "tests/more_languages/group4/go_test.go",
# ["type Greeting -> func (g Greeting) sayHello", "func createGreeting"],
# ),

# (
# "tests/more_languages/group4/haskell_test.hs",
# ["data Person", "greet :: Person -> String"],
Expand Down
48 changes: 41 additions & 7 deletions tree_plus_src/parse_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tree_plus_src.default_ignore import is_binary

# TODO: convert this to an environment variable and share across the modules
DEBUG = 0
DEBUG = 1


def debug_print(*args, **kwargs):
Expand Down Expand Up @@ -79,6 +79,14 @@ def parse_file(file_path: str) -> List[str]:
components = parse_angular_spec(contents) + components
elif file_extension == ".md":
components = parse_md(contents)
elif file_extension == ".c":
components = parse_c(contents)
elif file_extension in {".cpp", ".cc"}:
components = parse_cpp(contents)
elif file_extension == ".rs":
components = parse_rs(contents)
elif file_extension == ".go":
components = parse_go(contents)
elif file_extension == ".env":
components = parse_dot_env(contents)
elif file_extension == ".sql":
Expand Down Expand Up @@ -119,12 +127,6 @@ def parse_file(file_path: str) -> List[str]:
components = parse_powershell(contents)
elif file_extension == ".scala":
components = parse_scala(contents)
elif file_extension == ".c":
components = parse_c(contents)
elif file_extension in {".cpp", ".cc"}:
components = parse_cpp(contents)
elif file_extension == ".rs":
components = parse_rs(contents)
elif file_extension == ".tf":
components = parse_tf(contents)
elif file_extension in (".yml", ".yaml"):
Expand Down Expand Up @@ -161,6 +163,38 @@ def parse_cpp(contents) -> List[str]:
return components


def parse_go(contents) -> List[str]:
debug_print("parse_go")

# Combined regex pattern to match Go components
combined_pattern = re.compile(
# struct or type declarations without the body
r"\n(type \w+ (struct|interface)\s*)\{.*?\}\s*|"
# function declarations, including multiline, without the body
# r"\n(func[\s?\S?]*?){\n",
# r"\s(func [\s\S]+?){\s",
r"(func [\s\S]+?){\s",
re.DOTALL,
)

components = []

for match in combined_pattern.finditer(contents):
debug_print(f"{match=}")
debug_print(f"{match.groups()=}")
component = match.group().strip().replace(" {", "")
debug_print(f"{component=}")
# if component.startswith("func"):
# component = match.group(2).strip()
# component = component.replace(" {", "")
if component.startswith("type"):
component = match.group(1).strip()
debug_print(f"final {component=}")
components.append(component)

return components


# (declare|export) (default)?(\w+ \w+(<.*>)?(\(.*\))?(: \w+)?)
def parse_d_dot_ts(contents) -> List[str]:
debug_print(contents)
Expand Down

0 comments on commit c2e066d

Please sign in to comment.