generated from VLADIMIR/template
clear
This commit is contained in:
parent
186d09ba5a
commit
3612805009
@ -2,4 +2,5 @@ package cleaner
|
|||||||
|
|
||||||
type ICleaner interface {
|
type ICleaner interface {
|
||||||
ClearCode(code string) string
|
ClearCode(code string) string
|
||||||
|
ClearText(text string) string
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
package cleaner
|
package cleaner
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
replaceMap = map[string]string{
|
replaceMap = map[string]string{
|
||||||
@ -18,6 +21,7 @@ var (
|
|||||||
"b": "в",
|
"b": "в",
|
||||||
"u": "и",
|
"u": "и",
|
||||||
}
|
}
|
||||||
|
re = regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
type service struct{}
|
type service struct{}
|
||||||
@ -39,3 +43,7 @@ func (s *service) ClearCode(code string) string {
|
|||||||
}
|
}
|
||||||
return code
|
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)
|
clearCode := s.cleaner.ClearCode(code)
|
||||||
for _, place := range s.story.Places {
|
for _, place := range s.story.Places {
|
||||||
if s.cleaner.ClearCode(place.Code) == clearCode {
|
if s.cleaner.ClearCode(place.Code) == clearCode {
|
||||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
|
||||||
applications := make([]*Application, 0, len(place.Applications))
|
applications := make([]*Application, 0, len(place.Applications))
|
||||||
for _, application := range place.Applications {
|
for _, application := range place.Applications {
|
||||||
name := re.ReplaceAllString(application.Name, "")
|
name := s.cleaner.ClearText(application.Name)
|
||||||
applications = append(
|
applications = append(
|
||||||
applications,
|
applications,
|
||||||
&Application{
|
&Application{
|
||||||
@ -67,11 +66,10 @@ func (s *StoryService) GetPlace(code string) *Place {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
text := re.ReplaceAllString(place.Text, "")
|
|
||||||
return &Place{
|
return &Place{
|
||||||
Code: place.Code,
|
Code: place.Code,
|
||||||
Name: place.Name,
|
Name: place.Name,
|
||||||
Text: text,
|
Text: s.cleaner.ClearText(place.Text),
|
||||||
Applications: applications,
|
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 {
|
func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *GraphNode) error {
|
||||||
if code != "" && node.Code == "" {
|
if code != "" && node.Code == "" {
|
||||||
|
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 {
|
for i := range s.story.Places {
|
||||||
if s.story.Places[i].Code == code {
|
if s.story.Places[i].Code == code {
|
||||||
s.story.Places = append(s.story.Places[:i], s.story.Places[i+1:]...)
|
s.story.Places = append(s.story.Places[:i], s.story.Places[i+1:]...)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.Update(ctx)
|
return s.Update(ctx)
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
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))
|
nodeApplications := make([]*Application, 0, len(node.Applications))
|
||||||
for _, application := range node.Applications {
|
for _, application := range node.Applications {
|
||||||
nodeApplications = append(
|
nodeApplications = append(
|
||||||
@ -103,51 +153,7 @@ func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *Graph
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if code == "" && node.Code != "" {
|
return nodeApplications
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
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)
|
edges := make([]*GraphEdge, 0, len(s.story.Places)*3)
|
||||||
for _, place := range s.story.Places {
|
for _, place := range s.story.Places {
|
||||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
placeLinks := s.findPlaceLinksInText(place.Text)
|
||||||
matches := re.FindAllString(place.Text, -1)
|
for _, placeLink := range placeLinks {
|
||||||
|
|
||||||
for _, match := range matches {
|
|
||||||
edges = append(
|
edges = append(
|
||||||
edges,
|
edges,
|
||||||
&GraphEdge{
|
&GraphEdge{
|
||||||
From: m[s.cleaner.ClearCode(place.Code)],
|
From: m[s.cleaner.ClearCode(place.Code)],
|
||||||
To: m[s.cleaner.ClearCode(match)],
|
To: m[s.cleaner.ClearCode(placeLink)],
|
||||||
Type: "node",
|
Type: "node",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
for _, application := range place.Applications {
|
for _, application := range place.Applications {
|
||||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
placeLinks := s.findPlaceLinksInText(application.Name)
|
||||||
matches := re.FindAllString(application.Name, -1)
|
for _, placeLink := range placeLinks {
|
||||||
for _, match := range matches {
|
|
||||||
edges = append(
|
edges = append(
|
||||||
edges,
|
edges,
|
||||||
&GraphEdge{
|
&GraphEdge{
|
||||||
From: m[s.cleaner.ClearCode(place.Code)],
|
From: m[s.cleaner.ClearCode(place.Code)],
|
||||||
To: m[s.cleaner.ClearCode(match)],
|
To: m[s.cleaner.ClearCode(placeLink)],
|
||||||
Type: "application",
|
Type: "application",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -210,3 +213,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
|||||||
Edges: edges,
|
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