generated from VLADIMIR/template
clear
This commit is contained in:
parent
186d09ba5a
commit
3612805009
@ -2,4 +2,5 @@ package cleaner
|
||||
|
||||
type ICleaner interface {
|
||||
ClearCode(code string) string
|
||||
ClearText(text string) string
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package cleaner
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
replaceMap = map[string]string{
|
||||
@ -18,6 +21,7 @@ var (
|
||||
"b": "в",
|
||||
"u": "и",
|
||||
}
|
||||
re = regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
)
|
||||
|
||||
type service struct{}
|
||||
@ -39,3 +43,7 @@ func (s *service) ClearCode(code string) string {
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func (s *service) ClearText(text string) string {
|
||||
return re.ReplaceAllString(text, "")
|
||||
}
|
||||
|
||||
@ -41,3 +41,32 @@ func Test_service_ClearCode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_service_ClearText(t *testing.T) {
|
||||
tests := []struct {
|
||||
text string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
text: "text",
|
||||
want: "text",
|
||||
},
|
||||
{
|
||||
text: "text ([Ы])",
|
||||
want: "text",
|
||||
},
|
||||
{
|
||||
text: "text ([Ы-3])",
|
||||
want: "text",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.text, func(t *testing.T) {
|
||||
var s service
|
||||
got := s.ClearText(tt.text)
|
||||
if got != tt.want {
|
||||
t.Errorf("ClearText() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,10 +56,9 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
clearCode := s.cleaner.ClearCode(code)
|
||||
for _, place := range s.story.Places {
|
||||
if s.cleaner.ClearCode(place.Code) == clearCode {
|
||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
applications := make([]*Application, 0, len(place.Applications))
|
||||
for _, application := range place.Applications {
|
||||
name := re.ReplaceAllString(application.Name, "")
|
||||
name := s.cleaner.ClearText(application.Name)
|
||||
applications = append(
|
||||
applications,
|
||||
&Application{
|
||||
@ -67,11 +66,10 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
},
|
||||
)
|
||||
}
|
||||
text := re.ReplaceAllString(place.Text, "")
|
||||
return &Place{
|
||||
Code: place.Code,
|
||||
Name: place.Name,
|
||||
Text: text,
|
||||
Text: s.cleaner.ClearText(place.Text),
|
||||
Applications: applications,
|
||||
}
|
||||
}
|
||||
@ -85,15 +83,67 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
|
||||
func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *GraphNode) error {
|
||||
if code != "" && node.Code == "" {
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == code {
|
||||
s.story.Places = append(s.story.Places[:i], s.story.Places[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
s.Update(ctx)
|
||||
return s.deletePlace(ctx, code)
|
||||
}
|
||||
if code == "" && node.Code != "" {
|
||||
return s.addPlace(ctx, node)
|
||||
}
|
||||
if code == "" || node.Code == "" {
|
||||
return nil
|
||||
}
|
||||
return s.updatePlace(ctx, code, node)
|
||||
}
|
||||
|
||||
func (s *StoryService) deletePlace(ctx context.Context, code string) error {
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == code {
|
||||
s.story.Places = append(s.story.Places[:i], s.story.Places[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.Update(ctx)
|
||||
}
|
||||
|
||||
func (s *StoryService) addPlace(ctx context.Context, node *GraphNode) error {
|
||||
s.story.Places = append(
|
||||
s.story.Places,
|
||||
&Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: s.getApplications(node),
|
||||
},
|
||||
)
|
||||
return s.Update(ctx)
|
||||
}
|
||||
|
||||
func (s *StoryService) updatePlace(ctx context.Context, code string, node *GraphNode) error {
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == code {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: s.getApplications(node),
|
||||
}
|
||||
return s.Update(ctx)
|
||||
}
|
||||
}
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == node.Code {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: s.getApplications(node),
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.Update(ctx)
|
||||
}
|
||||
|
||||
func (s *StoryService) getApplications(node *GraphNode) []*Application {
|
||||
nodeApplications := make([]*Application, 0, len(node.Applications))
|
||||
for _, application := range node.Applications {
|
||||
nodeApplications = append(
|
||||
@ -103,51 +153,7 @@ func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *Graph
|
||||
},
|
||||
)
|
||||
}
|
||||
if code == "" && node.Code != "" {
|
||||
s.story.Places = append(
|
||||
s.story.Places,
|
||||
&Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
},
|
||||
)
|
||||
s.Update(ctx)
|
||||
return nil
|
||||
}
|
||||
if code == "" || node.Code == "" {
|
||||
return nil
|
||||
}
|
||||
update := false
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == code {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
}
|
||||
update = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !update {
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == node.Code {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: code,
|
||||
Name: node.Name,
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
}
|
||||
update = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Update(ctx)
|
||||
return nil
|
||||
return nodeApplications
|
||||
}
|
||||
|
||||
func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
@ -176,28 +182,25 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
|
||||
edges := make([]*GraphEdge, 0, len(s.story.Places)*3)
|
||||
for _, place := range s.story.Places {
|
||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
matches := re.FindAllString(place.Text, -1)
|
||||
|
||||
for _, match := range matches {
|
||||
placeLinks := s.findPlaceLinksInText(place.Text)
|
||||
for _, placeLink := range placeLinks {
|
||||
edges = append(
|
||||
edges,
|
||||
&GraphEdge{
|
||||
From: m[s.cleaner.ClearCode(place.Code)],
|
||||
To: m[s.cleaner.ClearCode(match)],
|
||||
To: m[s.cleaner.ClearCode(placeLink)],
|
||||
Type: "node",
|
||||
},
|
||||
)
|
||||
}
|
||||
for _, application := range place.Applications {
|
||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
matches := re.FindAllString(application.Name, -1)
|
||||
for _, match := range matches {
|
||||
placeLinks := s.findPlaceLinksInText(application.Name)
|
||||
for _, placeLink := range placeLinks {
|
||||
edges = append(
|
||||
edges,
|
||||
&GraphEdge{
|
||||
From: m[s.cleaner.ClearCode(place.Code)],
|
||||
To: m[s.cleaner.ClearCode(match)],
|
||||
To: m[s.cleaner.ClearCode(placeLink)],
|
||||
Type: "application",
|
||||
},
|
||||
)
|
||||
@ -210,3 +213,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
Edges: edges,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StoryService) findPlaceLinksInText(text string) []string {
|
||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
return re.FindAllString(text, -1)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user