Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
7c769cfa
Commit
7c769cfa
authored
4 months ago
by
Semyon Krestyaninov
Browse files
Options
Downloads
Patches
Plain Diff
Обновлена сигнатура `PublishFunc` для возможности условной публикации элементов
parent
ee199806
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/setup/item.go
+17
-6
17 additions, 6 deletions
pkg/setup/item.go
pkg/setup/item_test.go
+104
-0
104 additions, 0 deletions
pkg/setup/item_test.go
with
121 additions
and
6 deletions
pkg/setup/item.go
+
17
−
6
View file @
7c769cfa
...
...
@@ -26,7 +26,7 @@ type (
ItemOption
=
EntityOption
[
ItemConf
,
*
items
.
Item
]
ItemConf
struct
{
PublishFunc
func
(
s
*
Setup
,
item
*
items
.
Item
)
(
*
items
.
Item
,
bool
)
PublishFunc
func
(
s
*
Setup
,
item
*
items
.
Item
,
exists
bool
)
(
*
items
.
Item
,
bool
)
encoded
bool
// Если запись загружена из файла, необходимо выполнить Decode перед установкой
}
)
...
...
@@ -45,17 +45,26 @@ func (ItemConf) Init(e *Entity[ItemConf, *items.Item]) {
DeleteItemIfRemoveFlag
()(
e
)
}
//
OverwriteItem перезаписыва
ет элемент
//
PublishItem публику
ет элемент
.
func
PublishItem
()
ItemOption
{
return
func
(
c
*
Item
)
{
c
.
Conf
.
PublishFunc
=
func
(
s
*
Setup
,
item
*
items
.
Item
)
(
*
items
.
Item
,
bool
)
{
return
item
,
true
}
c
.
Conf
.
PublishFunc
=
func
(
_
*
Setup
,
item
*
items
.
Item
,
_
bool
)
(
*
items
.
Item
,
bool
)
{
return
item
,
true
}
}
}
// PublishItemIfNotExists публикует элемент, если он отсутствовал в системе.
func
PublishItemIfNotExists
()
ItemOption
{
return
func
(
c
*
Item
)
{
c
.
Conf
.
PublishFunc
=
func
(
_
*
Setup
,
item
*
items
.
Item
,
exists
bool
)
(
*
items
.
Item
,
bool
)
{
return
item
,
!
exists
}
}
}
// DraftItem не публикует элемент, сохраняет его в черновике
func
DraftItem
()
ItemOption
{
return
func
(
c
*
Item
)
{
c
.
Conf
.
PublishFunc
=
func
(
s
*
Setup
,
item
*
items
.
Item
)
(
*
items
.
Item
,
bool
)
{
return
item
,
false
}
c
.
Conf
.
PublishFunc
=
func
(
_
*
Setup
,
item
*
items
.
Item
,
_
bool
)
(
*
items
.
Item
,
bool
)
{
return
item
,
false
}
}
}
...
...
@@ -149,7 +158,9 @@ func (s *Setup) InstallItem(ctx context.Context, exists map[string]*items.Item,
exist
,
itemExists
:=
exists
[
item
.
ID
]
// Если элемент не существует, создаем его
if
!
itemExists
{
if
item
,
publish
:=
c
.
Conf
.
PublishFunc
(
s
,
item
);
publish
{
var
publish
bool
item
,
publish
=
c
.
Conf
.
PublishFunc
(
s
,
item
,
itemExists
)
if
publish
{
return
items
.
CreateAndPublishItem
(
ctx
,
s
.
content
.
Items
,
item
)
}
if
_
,
err
:=
s
.
content
.
Items
.
Create
(
ctx
,
item
);
err
!=
nil
{
...
...
@@ -160,7 +171,7 @@ func (s *Setup) InstallItem(ctx context.Context, exists map[string]*items.Item,
// Если элемент существует, обновляем его
if
item
,
changed
:=
c
.
UpdateFunc
(
s
,
exist
,
item
);
changed
{
if
_
,
publish
:=
c
.
Conf
.
PublishFunc
(
s
,
item
);
publish
{
if
_
,
publish
:=
c
.
Conf
.
PublishFunc
(
s
,
item
,
itemExists
);
publish
{
return
items
.
UpdateAndPublishItem
(
ctx
,
s
.
content
.
Items
,
item
)
}
if
err
:=
s
.
content
.
Items
.
Update
(
ctx
,
item
);
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
pkg/setup/item_test.go
+
104
−
0
View file @
7c769cfa
...
...
@@ -359,3 +359,107 @@ func TestSetup_UpdateDraft(t *testing.T) {
})
}
}
func
TestPublishItemIfNotExists
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
items
[]
*
items
.
Item
itemsCall
func
(
svc
*
itemsMock
.
Items
)
wantErr
assert
.
ErrorAssertionFunc
}{
{
name
:
"Item not exists"
,
items
:
[]
*
items
.
Item
{
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{
"text"
:
"test"
},
},
},
itemsCall
:
func
(
svc
*
itemsMock
.
Items
)
{
svc
.
On
(
"Find"
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
nil
,
0
,
nil
)
.
Once
()
svc
.
On
(
"Create"
,
mock
.
Anything
,
&
items
.
Item
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{
"text"
:
"test"
},
})
.
Return
(
&
items
.
Item
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{
"text"
:
"test"
},
},
nil
)
.
Once
()
svc
.
On
(
"Publish"
,
mock
.
Anything
,
&
items
.
Item
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{
"text"
:
"test"
},
})
.
Return
(
nil
)
.
Once
()
},
wantErr
:
assert
.
NoError
,
},
{
name
:
"Item exists"
,
items
:
[]
*
items
.
Item
{
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{},
},
},
itemsCall
:
func
(
svc
*
itemsMock
.
Items
)
{
svc
.
On
(
"Find"
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
([]
*
items
.
Item
{{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{
"text"
:
"test"
},
}},
1
,
nil
)
.
Once
()
svc
.
On
(
"Update"
,
mock
.
Anything
,
&
items
.
Item
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{},
})
.
Return
(
nil
)
.
Once
()
svc
.
On
(
"Unpublish"
,
mock
.
Anything
,
&
items
.
Item
{
ID
:
"1"
,
SpaceID
:
"space"
,
EnvID
:
"env"
,
CollectionID
:
"coll"
,
Data
:
map
[
string
]
any
{},
})
.
Return
(
nil
)
.
Once
()
},
wantErr
:
assert
.
NoError
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
svc
:=
itemsMock
.
NewItems
(
t
)
if
tt
.
itemsCall
!=
nil
{
tt
.
itemsCall
(
svc
)
}
s
:=
NewSetup
(
&
content
.
Content
{
Items
:
svc
},
"space"
,
"env"
,
nil
)
s
.
AddItems
(
tt
.
items
,
PublishItemIfNotExists
(),
// Добавляем опцию обновления для проверки публикации, когда элемент уже существует
func
(
c
*
Entity
[
ItemConf
,
*
items
.
Item
])
{
c
.
UpdateFunc
=
func
(
_
*
Setup
,
_
,
item
*
items
.
Item
)
(
*
items
.
Item
,
bool
)
{
return
item
,
true
}
},
)
tt
.
wantErr
(
t
,
s
.
InstallItems
(
context
.
Background
()))
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment