Skip to content

Commit

Permalink
Поправил замечания
Browse files Browse the repository at this point in the history
  • Loading branch information
thedemoncat committed May 3, 2021
1 parent 5e2a2ad commit 4b491df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
31 changes: 17 additions & 14 deletions mdoTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,38 +194,41 @@ func (e MDOTypeRefList) Unpack(cfg UnpackConfig, value interface{}) error {
}

// Проверяет существование объекта.
func (e MDOTypeRefList) Exist(typeRef *MDOTypeRef) bool {
for _, t := range e {
if typeRef.raw != "" && t.raw == typeRef.raw {
return true
}
if t.mdoType == typeRef.Type() && t.ref == typeRef.Ref() {
return true
func (e MDOTypeRefList) Exist(typeRef MDOTypeRef) bool {

if typeRef.raw != "" {
return e.GetIndex(typeRef.raw) > -1
}
if typeRef.mdoType != "" && typeRef.ref != "" {
for _, t := range e {
if t.mdoType == typeRef.Type() && t.ref == typeRef.Ref() {
return true
}
}
}

return false
}

// Возвращает индекс элемента в массиве
func (e MDOTypeRefList) GetIndex(raw string) (int, error) {
for i,t := range e{
func (e MDOTypeRefList) GetIndex(raw string) int {
for i, t := range e {
if raw != "" && t.raw == raw {
return i, nil
return i
}
}
return 0, errors.New(fmt.Sprintf("Object not contains %s", raw))
return -1
}


// Удаляет элемент по индексу
func (e MDOTypeRefList) Delete(index int) ([]MDOTypeRef, error) {
if len(e) > index {
return RemoveElement(e, index), nil
return removeElement(e, index), nil
}
return e, errors.New(fmt.Sprintf("Error delete object from index %v", index))
}

func RemoveElement(list []MDOTypeRef, i int) []MDOTypeRef {
func removeElement(list []MDOTypeRef, i int) []MDOTypeRef {
if i < len(list)-1 {
list = append(list[:i], list[i+1:]...)
} else {
Expand Down
10 changes: 5 additions & 5 deletions mdoTypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ func TestMDOTypeRefExist(t *testing.T) {
parent: nil,
raw: "Subsystem.ПерваяПодсистема",
}
require.True(t,got.ConfigurationChildObjects.Subsystems.Exist(&subsystem))
require.True(t, got.ConfigurationChildObjects.Subsystems.Exist(subsystem))
}

func TestMDOTypeRefGetIndex(t *testing.T) {
func TestMDOTypeRefGetIndex(t *testing.T) {
got, err := UnpackConfiguration("tests/metadata/edt/src")
if err != nil {
t.Errorf("UnpackConfiguration() error = %v", err)
return
}
index, err := got.ConfigurationChildObjects.Subsystems.GetIndex("Subsystem.ВтораяПодсистема")
if err != nil {
index := got.ConfigurationChildObjects.Subsystems.GetIndex("Subsystem.ВтораяПодсистема")
if index < 0 {
t.Errorf("Ошибка поиска дочернего объекта = %v", err)
}
require.True(t, index == 1)
Expand All @@ -98,4 +98,4 @@ func TestMDOTypeRefDelete(t *testing.T) {
}
got.ConfigurationChildObjects.Subsystems = newChild
require.True(t, len(got.ConfigurationChildObjects.Subsystems) == 1)
}
}

0 comments on commit 4b491df

Please sign in to comment.