Skip to content

mock: replace objx.Map with our own mock.TestData type to drop objx #1852

@dolmen

Description

@dolmen

Description

To drop dependency on module objx (see #1752) I propose to replace objx.Map with an internal implementation that exposes a similar (subset) API to objx.Map that is just enough for the few users of Mock.TestData.

Proposed solution

My solution with our own mock.TestData and mock.TestDataValue types that exposes the subset of objx.Map methods used in the wild.

  1. Merge mock: isolate objx-dependent code in their own source files #1757.
  2. Add types TestData and TestDataValue.
  3. Replace use of objx.Map with mock.TestData in the signature of Mock.TestData().
package mock

import "reflect"

// TestData replaces [github.com/stretchr/objx.Map].
type TestData map[string]interface{}

type reflectValue = reflect.Value

// TestDataValue replaces [github.com/stretchr/objx.Value] and exposes the same methods as [reflect.Value].
type TestDataValue struct {
    reflectValue
}

// Set replaces [github.com/stretchr/objx.Map.Set].
func (td TestData) Set(selector string, v interface{}) {
    td[selector] = v
}

// Get replaces [github.com/stretchr/objx.Map.Get].
func (td TestData) Get(selector string) *TestDataValue {
    v, ok := td[selector]
    if !ok {
         return nil
    }
    return &TestDataValue{reflectValue: reflect.ValueOf(&v).Elem()}
}

// MustInter replaces [github.com/stretchr/objx.Value.MustInter].
func (v *TestDataValue) MustInter() interface{} {
    if v == nil {
        return nil
    }
    return v.reflectValue.Interface()
}


func (m *Mock) TestData() objx.Map {
        if m.testData == nil {
                m.testData = make(TestData)
        }

        return m.testData
}

Use case

#1752

Metadata

Metadata

Assignees

Labels

dependenciesPull requests that update a dependency filepkg-mockAny issues related to Mock

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions