compose-spec / compose-go

Reference library for parsing and loading Compose YAML files

Home Page:https://compose-spec.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve usability of Project & Service objects

milas opened this issue · comments

Description

The Project and Service objects returned by this library often
confuse users because certain fields are not always initialized.

For example, CustomLabels (a map[string]string) will be nil
on each Service.

Potential Solutions

  • Add NewProject and NewService constructors that initialize
    all fields consistently
  • Add helper methods, e.g. AddCustomLabel that internally handle
    initialization lazily

Related Issues

docker/compose#9808

cc @glours I know we talked about this before around another Compose PR, so you might remember some other cases we should track here

Users should add new elements to this kind of Map with the helper Add function that checks if the map is empty or not before adding a new value.

Anyway we can create a generic function for that as Golang 1.19 is out.
Golang 1.17 is not supported anymore so we may assume downstream projects to use at least Golang v1.18.x

Hello guys, sorry for joining the discussion here 🙏 .

how about on LoadService we add the process to initialize the CustomLabel to the empty value of map[string]string instead of leaving it nil ?
because most who use this compose-go library call it in loader.Load(). so when loader.Load() return the Project type the value of Service.CustomLabel not nil but already initialize.