Skip to content

Commit

Permalink
make Services a map[string]ServiceConfig to reflect yaml structure
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 21, 2023
1 parent 593b777 commit f2a0f5d
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 286 deletions.
9 changes: 5 additions & 4 deletions cli/options_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func TestConvertWithEnvVar(t *testing.T) {
p, err := ProjectFromOptions(opts)

assert.NilError(t, err)
assert.Equal(t, len(p.Services[0].Volumes), 3)
assert.Equal(t, p.Services[0].Volumes[0].Source, "/c/docker/project")
assert.Equal(t, p.Services[0].Volumes[1].Source, "/c/project-dir/relative")
assert.Equal(t, p.Services[0].Volumes[2].Source, "/c/project-dir/relative2")
volumes := p.Services["test"].Volumes
assert.Equal(t, len(volumes), 3)
assert.Equal(t, volumes[0].Source, "/c/docker/project")
assert.Equal(t, volumes[1].Source, "/c/project-dir/relative")
assert.Equal(t, volumes[2].Source, "/c/project-dir/relative2")
}
8 changes: 4 additions & 4 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func fullExampleProject(workingDir, homeDir string) *types.Project {
}
}

func services(workingDir, homeDir string) []types.ServiceConfig {
return []types.ServiceConfig{
{
func services(workingDir, homeDir string) types.Services {
return types.Services{
"foo": {
Name: "foo",

Annotations: map[string]string{
Expand Down Expand Up @@ -436,7 +436,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
},
WorkingDir: "/code",
},
{
"bar": {
Name: "bar",
Build: &types.BuildConfig{
Context: workingDir,
Expand Down
23 changes: 9 additions & 14 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ func Transform(source interface{}, target interface{}) error {
data := mapstructure.Metadata{}
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
nameServices,
decoderHook,
makeServiceSlice,
cast),
Result: target,
TagName: "yaml",
Expand All @@ -594,21 +594,16 @@ func Transform(source interface{}, target interface{}) error {
return decoder.Decode(source)
}

func makeServiceSlice(from reflect.Value, to reflect.Value) (interface{}, error) {
// nameServices create implicit `name` key for convenience accessing service
func nameServices(from reflect.Value, to reflect.Value) (interface{}, error) {
if to.Type() == reflect.TypeOf(types.Services{}) {
keys := from.MapKeys()
services := make([]any, len(keys))
i := 0
for _, key := range keys {
service := from.MapIndex(key).Elem()
if service.Kind() != reflect.Map {
return nil, fmt.Errorf("unexpected service type %T", service)
}
service.SetMapIndex(reflect.ValueOf("name"), key)
services[i] = service.Interface()
i++
nameK := reflect.ValueOf("name")
iter := from.MapRange()
for iter.Next() {
name := iter.Key()
elem := iter.Value()
elem.Elem().SetMapIndex(nameK, name)
}
return services, nil
}
return from.Interface(), nil
}
Expand Down
Loading

0 comments on commit f2a0f5d

Please sign in to comment.