Skip to content

Policy for default values in CUE API #1243

Closed

Description

@shykes

Overview

I propose setting the following design principle for our CUE APIs:

Dagger CUE APIs should define default values explicitly, rather than relying on implied default values from other APIs they depend on.
For example, our CUE bindings to Buildkit calls should explicitly define default values in CUE, rather than rely on Buildkit’s own defaults.
This CUE-first approach to default values improves the developer experience in several ways:

  1. It makes our CUE APIs more explicit
  2. It improves API documentation, since the defaults can be captured by our docs generation tools
  3. API authors can take fuller responsibility for the stability of their API, instead of depending on the stability of dependencies outside of their control.

Activity

  1. added this to the milestone on Dec 17, 2021
  2. modified the milestones: , next on Jan 7, 2022
  3. aluzzardi commented on Jan 7, 2022

    @aluzzardi
    Contributor

    Makes sense to me. @jlongtine @talentedmrjones ?

    @shykes Would it make sense to start a document in the repo to encode such policies?

  4. self-assigned this
    on Jan 10, 2022
  5. shykes commented on Jan 10, 2022

    @shykes
    ContributorAuthor

    Yes agreed. I will take this and combine with #1360.

  6. helderco commented on Jan 20, 2022

    @helderco
    Contributor

    Working on #1247, it became clear that trying to set defaults in docker.#Run, on top of engine.#Exec doesn't work.

    In discord, I proposed setting defaults for tasks in the Go code that handles them. That would free abstractions in CUE to set their defaults differently from engine tasks.

    @shykes replied:

    Yeah combining defaults from multiple layers is an issue in CUE in general

    My thinking is:

    • Ideally we could handle this in pure CUE. I consider it a limitation in the language that we can’t.
    • I think it’s fine to implement a workaround where we do this in Go. But it should be explained as such. Perhaps with FIXMEs etc. so we don’t lose track of the fact that, in a perfect world, it would be in CUE
    • We open an issue to track this particular problem, and keep it open so we can share it with upstream CUE devs, in the hope that down the road they provide a solution in the language to remove the workaround

    I've looked around and this is what Marcel has to say:

    This touches upon a fundamental aspect and design goals of CUE, namely that one should avoid having more than one level of inheritance at all cost, as this is one of the key reasons why configuration becomes unmanageable. Default values are a form of inheritance. As such CUE is particularly restrictive there.

    [...]

    Rather than fixing this by changing the language, it will be mostly a matter of education of patterns. Generally it is good practice for base schema definitions to not have defaults. Instead, defaults can be defined in a separate value that can be "composed in" as needed.

    [...]

    Originally posted by @mpvl in cue-lang/cue#1449 (comment)

    I do have a workaround in CUE though:

    #Run: {
    	_image: #Image
    
    	user: string
    	if _image.config.User != _|_ {
    		if user != "root" {
    			if _image.config.User != "root" {
    				user: *_image.config.User | !="root"
    			}
    		}
    	}
    }

    You can essentially remove a default by making it evaluate to _|_, but in this case you have other things to consider as well.

    Sometimes though, I feel that we try to do too much in CUE which is stretching the language beyond it's design philosophy.

    In light of all this, I still think the cleaner solution is to set task defaults in Go code, but not as a temporary workaround. In CUE, you can document the field ending in something like // ... (defaults to "root").

    /cc @jlongtine

  7. talentedmrjones commented on Jan 20, 2022

    @talentedmrjones

    I agree, and in setting those defaults we should prefer *<value> | <type> rather than using if {} statements wherever possible since they are more easily documented and gleened.

  8. helderco commented on Jan 20, 2022

    @helderco
    Contributor

    @talentedmrjones Yes, that would become possible if #Exec doesn't have a default in CUE:

    #Exec: {
    	// User ID or name (defaults to "root")
    	user: string 
    }
    
    #Run: {
    	_image: #Image
    	user: *_image.config.User | string
    }
  9. shykes commented on Jan 21, 2022

    @shykes
    ContributorAuthor
    • I agree with moving task defaults to Go, as a practical matter
    • I do think this will become a problem for CUE developers as the ecosystem grows, because it basically means package developers cannot safely set defaults in their package unless they are 100% sure that their package will not be wrapped in a higher-level package that will want to set their own default. But, it’s often impossible to know that for sure.
    • TLDR I don’t know what the new defaults policy should be for package developers. What’s the rule?
  10. helderco commented on Jan 21, 2022

    @helderco
    Contributor

    CUE's recommendation is to not have defaults and compose with unification. The issue is that it looks a bit ugly (or the DX is less great)? 😅

    --- docker package ---
    
    ImageDefaults: {
        user: *image.config.User | string
    }
    
    Default: {
        user: *"root" | string
    }
    
    #BaseRun: {
        // #Run with no defaults
    }
    
    #Run: #BaseRun & Defaults {
       ...
    }
    
    --- plan ---
    
    run1: docker.#Run & {
       ...
    }
    
    run2: docker.#BaseRun & ImageDefaults & {
       ...
    }

    In the case of #ImageConfig though, you're not guaranteed to to have User in there, so setting exec's defaults in Go is still the best option here.

    But this is essentially what I'm doing in my compose.yaml generation use case, and in that case I don't think it's ugly 😁 It feels natural to be composing in this way.

    Example:

    Php: {
        // no defaults here
    }
    
    _PhpLegacy: Php & {
        // also no defaults here
    }
    
    Php56: _PhpLegacy & {
        // now we're talking
        services: app: image: string | *"<full ref here>"
    }
    
    Php74: Php & {
        services: app: image: string | *"<full ref here>"
    }
    
    _Bolt: Php56 & {
        services: {
            // end of the line for you!
            web: image: "<full ref here>"
        }
    }
    
    app1: Php56 & {
        ... 
    }
    
    app2: _Bolt & lib.Msmtp & {
        // here's an smtp mixin for you
    }
    

    Point is, I'm deciding when I'm introducing defaults based on my needs. If I'd need to base something else on top of _Bolt, I'd have to change that. But whenever you introduce it for others, you're limiting them. Unless you provide a base to extend, with accompanying mixins to compose (or not), and perhaps a case for most common use (e.g., #Run).

  11. modified the milestones: v0.2.0-alpha.3, next on Jan 31, 2022
  12. aluzzardi commented on Jan 31, 2022

    @aluzzardi
    Contributor

    Rolled over into the next milestone

  13. shykes commented on Feb 11, 2022

    @shykes
    ContributorAuthor

    My initial proposal is out of date, because it didn't account for the limitations in how CUE handles default values. This issue is addressed in #857 (point 10). Closing this since it's not immediately actionable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions