Skip to content

Commit 3bcc61f

Browse files
Merge pull request #89 from fabianoflorentino/development
Development to Main
2 parents f066d29 + c20146c commit 3bcc61f

11 files changed

Lines changed: 228 additions & 159 deletions

File tree

internal/agrupamento_de_dados/constants.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const (
2929
flagSliceLiteralComposta string = "--slice-literal-composta"
3030
flagSliceForRange string = "--slice-for-range"
3131
flagSliceFatiandoOuDeletando string = "--slice-fatiando-ou-deletando-de-uma-fatia"
32-
flagSliceFatiandoOuDeletandoRes string = "--slice-fatiando-ou-deletando-de-uma-fatia --resolucao"
3332
flagSliceAnexando string = "--slice-anexando-a-uma-slice"
3433
flagSliceMake string = "--slice-make"
3534
flagSliceMultiDimensional string = "--slice-multi-dimensional"

internal/agrupamento_de_dados/help.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
package agrupamento_de_dados
88

99
import (
10-
"fmt"
11-
10+
base "github.com/fabianoflorentino/aprendago/pkg/base_content"
1211
"github.com/fabianoflorentino/aprendago/pkg/format"
1312
)
1413

@@ -23,7 +22,6 @@ func Help() {
2322
{Flag: flagSliceLiteralComposta, Description: descSliceLiteralComposta},
2423
{Flag: flagSliceForRange, Description: descSliceForRange},
2524
{Flag: flagSliceFatiandoOuDeletando, Description: descSliceFatiandoOuDeletando},
26-
{Flag: flagSliceFatiandoOuDeletandoRes, Description: descSliceFatiandoOuDeletandoRes},
2725
{Flag: flagSliceAnexando, Description: descSliceAnexando},
2826
{Flag: flagSliceMake, Description: descSliceMake},
2927
{Flag: flagSliceMultiDimensional, Description: descSliceMultiDimensional},
@@ -32,6 +30,6 @@ func Help() {
3230
{Flag: flagMapsRangeEDeletando, Description: descMapsRangeEDeletando},
3331
}
3432

35-
fmt.Println("Capítulo 8: Agrupamento de Dados")
36-
format.PrintHelpMe(h)
33+
b := base.New()
34+
b.HelpMe("Capítulo 8: Agrupamento de Dados", h)
3735
}
Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
11
package agrupamento_de_dados
22

33
import (
4+
// "go/format" removed as it does not contain MenuOptions
5+
6+
base "github.com/fabianoflorentino/aprendago/pkg/base_content"
47
"github.com/fabianoflorentino/aprendago/pkg/format"
5-
"github.com/fabianoflorentino/aprendago/pkg/logger"
6-
"github.com/fabianoflorentino/aprendago/pkg/section"
78
)
89

9-
// menu generates a list of menu options based on the provided menu slice and root directory.
10-
// It initializes a section using the given root directory and returns a slice of MenuOptions,
11-
// each containing an option string and an associated execution function.
12-
//
13-
// Parameters:
14-
// - menu: A slice of strings representing the menu options.
15-
// - rootDir: A string representing the root directory for initializing the section.
16-
//
17-
// Returns:
18-
// - A slice of format.MenuOptions, where each option has an associated execution function.
19-
// If an error occurs during section initialization, it logs the error and returns nil.
2010
func Menu(menu []string) []format.MenuOptions {
21-
section, err := section.New(rootDir)
22-
if err != nil {
23-
logger.New("Error creating section: ", err.Error()).Register()
11+
m := base.New()
12+
13+
newFlag := []string{
14+
flagArray,
15+
flagSliceLiteralComposta,
16+
flagSliceForRange,
17+
flagSliceFatiandoOuDeletando,
18+
flagSliceAnexando,
19+
flagSliceMake,
20+
flagSliceMultiDimensional,
21+
flagSliceSurpresaArraySubjacente,
22+
flagMapsIntroducao,
23+
flagMapsRangeEDeletando,
2424
}
2525

26-
return []format.MenuOptions{
27-
{Options: flagArray, ExecFunc: func() { sectionsAgrupamentoDeDados(array, section) }},
28-
{Options: flagSliceLiteralComposta, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceLiteralComposta, section) }},
29-
{Options: flagSliceForRange, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceForRange, section) }},
30-
{Options: flagSliceFatiandoOuDeletando, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceFatiandoOuDeletando, section) }},
31-
{Options: flagSliceAnexando, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceAnexando, section) }},
32-
{Options: flagSliceMake, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceMake, section) }},
33-
{Options: flagSliceMultiDimensional, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceMultiDimensional, section) }},
34-
{Options: flagSliceSurpresaArraySubjacente, ExecFunc: func() { sectionsAgrupamentoDeDados(sliceSurpresaArraySubjacente, section) }},
35-
{Options: flagMapsIntroducao, ExecFunc: func() { sectionsAgrupamentoDeDados(mapsIntroducao, section) }},
36-
{Options: flagMapsRangeEDeletando, ExecFunc: func() { sectionsAgrupamentoDeDados(mapsRangeEDeletando, section) }},
26+
newExecFunc := []func(){
27+
func() { m.SectionFormat(array, m.SectionFactory(rootDir)) },
28+
func() { m.SectionFormat(sliceLiteralComposta, m.SectionFactory(rootDir)) },
29+
func() { m.SectionFormat(sliceForRange, m.SectionFactory(rootDir)) },
30+
func() { m.SectionFormat(sliceFatiandoOuDeletando, m.SectionFactory(rootDir)) },
31+
func() { m.SectionFormat(sliceAnexando, m.SectionFactory(rootDir)) },
32+
func() { m.SectionFormat(sliceMake, m.SectionFactory(rootDir)) },
33+
func() { m.SectionFormat(sliceMultiDimensional, m.SectionFactory(rootDir)) },
34+
func() { m.SectionFormat(sliceSurpresaArraySubjacente, m.SectionFactory(rootDir)) },
35+
func() { m.SectionFormat(mapsIntroducao, m.SectionFactory(rootDir)) },
36+
func() { m.SectionFormat(mapsRangeEDeletando, m.SectionFactory(rootDir)) },
3737
}
38-
}
3938

40-
// sectionsAgrupamentoDeDados formats and processes sections of data under a given title.
41-
// It utilizes the SectionProvider interface to handle the formatting logic.
42-
//
43-
// Parameters:
44-
// - title: A string representing the title for the sections.
45-
// - sections: An implementation of the section.SectionProvider interface
46-
// that provides the functionality to format the sections.
47-
func sectionsAgrupamentoDeDados(title string, sections section.SectionProvider) {
48-
sections.Format(title)
39+
return m.Menu(newFlag, newExecFunc)
4940
}

internal/agrupamento_de_dados/menu_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func TestMenu(t *testing.T) {
2020
flagSliceLiteralComposta,
2121
flagSliceForRange,
2222
flagSliceFatiandoOuDeletando,
23-
flagSliceFatiandoOuDeletandoRes,
2423
flagSliceAnexando,
2524
flagSliceMake,
2625
flagSliceMultiDimensional,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package agrupamento_de_dados
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
type MockContentsProvider struct {
9+
CalledWithRootDir string
10+
CalledWithTopics []string
11+
}
12+
13+
func (m *MockContentsProvider) TopicsContents(rootDir string, topics []string) {
14+
m.CalledWithRootDir = rootDir
15+
m.CalledWithTopics = topics
16+
}
17+
18+
func TestTopicsAgrupamentoDeDados(t *testing.T) {
19+
os.Setenv("GOENV", "test")
20+
21+
rootDir := os.Getenv("GOENV")
22+
23+
if rootDir != "test" {
24+
t.Errorf("Expected rootDir to be 'test', got '%s'", rootDir)
25+
}
26+
27+
mockProvider := &MockContentsProvider{}
28+
mockProvider.TopicsContents(rootDir, []string{"topic1", "topic2"})
29+
30+
if mockProvider.CalledWithRootDir != rootDir {
31+
t.Errorf("Expected rootDir to be 'test', got '%s'", mockProvider.CalledWithRootDir)
32+
}
33+
34+
if len(mockProvider.CalledWithTopics) != 2 {
35+
t.Errorf("Expected 2 topics, got %d", len(mockProvider.CalledWithTopics))
36+
}
37+
}

internal/agrupamento_de_dados/topics.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ import (
1919
func Topics() {
2020
fmt.Printf("\nCapítulo 8: Agrupamento de Dados\n")
2121

22-
content := topic.New()
23-
contentsAgrupamentoDeDados(rootDir, content)
24-
}
25-
26-
// contentsAgrupamentoDeDados processes and organizes the contents of the
27-
// "agrupamento de dados" (data grouping) topics. It initializes a new
28-
// topic structure, retrieves the list of topics related to "agrupamento de dados",
29-
// and populates the topics' contents from the specified root directory.
30-
func contentsAgrupamentoDeDados(rootDir string, contents topic.ContentsProvider) {
31-
contents.TopicsContents(rootDir, listOfTopicsAgrupamentoDeDados())
32-
}
33-
34-
// listOfTopicsAgrupamentoDeDados returns a slice of strings containing
35-
// the topics related to "agrupamento de dados" (data grouping) in Go.
36-
func listOfTopicsAgrupamentoDeDados() []string {
3722
list := []string{
3823
array,
3924
sliceLiteralComposta,
@@ -47,15 +32,6 @@ func listOfTopicsAgrupamentoDeDados() []string {
4732
mapsRangeEDeletando,
4833
}
4934

50-
return listOfTopics(list)
51-
}
52-
53-
// listOfTopics takes a slice of strings as input and returns a new slice of strings.
54-
// The returned slice is initialized with a capacity of 10 and contains all the elements
55-
// from the input slice.
56-
func listOfTopics(inputList []string) []string {
57-
outputList := make([]string, 0, 10)
58-
outputList = append(outputList, inputList...)
59-
60-
return outputList
35+
content := topic.New()
36+
content.TopicsContents(rootDir, content.ListOfTopics(list, 10))
6137
}

internal/agrupamento_de_dados/topics_test.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

pkg/base_content/base_content.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Package basecontent provides foundational structures and functionalities
2+
// that can be extended or embedded by other types to enable shared behavior
3+
// and common operations. It includes utilities for generating menu options,
4+
// formatting sections, and creating section instances, making it a versatile
5+
// component for building modular and reusable Go applications.
6+
package basecontent
7+
8+
import (
9+
"fmt"
10+
11+
"github.com/fabianoflorentino/aprendago/pkg/format"
12+
"github.com/fabianoflorentino/aprendago/pkg/logger"
13+
"github.com/fabianoflorentino/aprendago/pkg/section"
14+
)
15+
16+
// MenuOptions defines an interface for handling menu options.
17+
// It provides a method to generate a list of menu options based on a flag
18+
// and a corresponding function to execute for the selected option.
19+
//
20+
// Menu takes two parameters:
21+
// - optionFlag: A string representing the flag or identifier for the menu option.
22+
// - optionFunc: A function to be executed when the menu option is selected.
23+
//
24+
// The method returns a slice of format.MenuOptions, representing the available menu options.
25+
type MenuOptions interface {
26+
Menu(optionFlag string, optionFunc func()) []format.MenuOptions
27+
}
28+
29+
type HelpMe interface {
30+
BuildHelpMe(title string, helpString []format.HelpMe)
31+
}
32+
33+
// BaseContent represents a foundational structure or entity that can be
34+
// extended or embedded by other types to provide shared functionality
35+
// or common behavior.
36+
type BaseContent struct{}
37+
38+
// New creates and returns a new instance of BaseContent.
39+
// This function initializes a BaseContent struct with default values.
40+
func New() *BaseContent {
41+
return &BaseContent{}
42+
}
43+
44+
// Menu generates a list of menu options based on the provided flags and corresponding functions.
45+
// Each menu option is represented as a format.MenuOptions object.
46+
//
47+
// Parameters:
48+
// - optionFlags: A slice of strings representing the flags or labels for the menu options.
49+
// - optionFuncs: A slice of functions corresponding to each menu option, defining the actions to be executed.
50+
//
51+
// Returns:
52+
// - A slice of format.MenuOptions containing the generated menu options.
53+
func (b *BaseContent) Menu(optionFlags []string, optionFuncs []func()) []format.MenuOptions {
54+
menuOptions := []format.MenuOptions{}
55+
56+
for i := 0; i < len(optionFlags) && i < len(optionFuncs); i++ {
57+
menuOptions = append(menuOptions, format.MenuOptions{Options: optionFlags[i], ExecFunc: optionFuncs[i]})
58+
}
59+
60+
return menuOptions
61+
}
62+
63+
// SectionFormat formats a section with the given title and applies it to the BaseContent.
64+
// It takes a title string and a pointer to a Section object as parameters.
65+
// The function modifies the BaseContent based on the provided section details.
66+
func (b *BaseContent) SectionFormat(title string, section *section.Section) {
67+
section.Format(title)
68+
}
69+
70+
// SectionFactory creates and returns a new Section instance based on the provided root directory.
71+
// It utilizes the BaseContent receiver to configure or initialize the Section as needed.
72+
//
73+
// Parameters:
74+
// - rootDir: The root directory path used to initialize the Section.
75+
//
76+
// Returns:
77+
// - A pointer to a Section instance configured with the given root directory.
78+
func (b *BaseContent) SectionFactory(rootDir string) *section.Section {
79+
section, err := section.New(rootDir)
80+
if err != nil {
81+
logger.New("Error creating section: ", err.Error()).Register()
82+
return nil
83+
}
84+
85+
return section
86+
}
87+
88+
// HelpMe prints a title and a formatted help message to the console.
89+
// It takes a title string and a slice of format.HelpMe objects, which
90+
// are used to generate the help message.
91+
//
92+
// Parameters:
93+
// - title: A string representing the title to be printed.
94+
// - helpString: A slice of format.HelpMe objects containing the help
95+
// information to be formatted and displayed.
96+
func (b *BaseContent) HelpMe(title string, helpString []format.HelpMe) {
97+
fmt.Println(title)
98+
format.PrintHelpMe(helpString)
99+
}

0 commit comments

Comments
 (0)