cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.12k stars 291 forks source link

cmd/cue: trim support for embedded definitions #766

Open cueckoo opened 3 years ago

cueckoo commented 3 years ago

Originally opened by @myitcv in https://github.com/cuelang/cue/issues/766

I have a number of definitions in base.cue:

package stats_api

import (
    "k8s.io/api/core/v1"
)

#CommonLabels: {
    app:     string
    release: string
    ...
}

#BaseApp: {
    _CommonLabels: #CommonLabels
    _Name:         string | *"\(_CommonLabels.release)-\(_CommonLabels.app)"

    serviceAccount: v1.#ServiceAccount & {
        apiVersion: "v1"
        kind:       "ServiceAccount"
        metadata: {
            labels: {
                _CommonLabels
                ...
            }
            name: _Name
        }
    }
}

#IngressGCE: {
    _CommonLabels: #CommonLabels
    _Name:         string | *"\(_CommonLabels.release)-\(_CommonLabels.app)"

    backendConfig: {
        apiVersion: "cloud.google.com/v1beta1"
        kind:       "BackendConfig"
        metadata: {
            labels: {
                _CommonLabels
                ...
            }
            name: _Name
        }
    }
}

That I reference from stats-api.cue:

package stats_api

stats_api: [Release=_]: {
    #BaseApp
    #IngressGCE

    _CommonLabels: #CommonLabels & {
        app:     "stats-api"
        release: Release
    }

    backendConfig: spec: {
        connectionDraining: drainingTimeoutSec: 300
        timeoutSec: 300
    }
}

Finally, I've imported the following test.cue from YAML:

package stats_api

stats_api: test: serviceAccount: {
    apiVersion: "v1"
    kind:       "ServiceAccount"
    metadata: {
        labels: {
            app:     "stats-api"
            release: "test"
        }
        name: "test-stats-api"
    }
}
stats_api: test: backendConfig: {
    apiVersion: "cloud.google.com/v1beta1"
    kind:       "BackendConfig"
    metadata: {
        labels: {
            app:     "stats-api"
            release: "test"
        }
        name: "test-stats-api"
    }
    spec: {
        connectionDraining: drainingTimeoutSec: 300
        timeoutSec: 300
    }
}

After I run cue trim -s ./... I expect test.cue to contain something like:

package stats_api

mlb_app: "stats-api": test: {}
mlb_app: "stats-api": test: {}

or, better still:

package stats_api

mlb_app: "stats-api": test: {}

but am actually left with:

package stats_api

stats_api: test: serviceAccount: {
    apiVersion: "v1"
    kind:       "ServiceAccount"
    metadata: {
        labels: {
            app:     "stats-api"
            release: "test"
        }
        name: "test-stats-api"
    }
}
stats_api: test: backendConfig: {
    apiVersion: "cloud.google.com/v1beta1"
    kind:       "BackendConfig"
    metadata: {
        labels: {
            app:     "stats-api"
            release: "test"
        }
        name: "test-stats-api"
    }
}

So only stats_api: test: backendConfig: spec has been trimmed.

Feature, bug, or PEBCAK?

trim.tar.gz

Originally posted by @mgoodness in https://github.com/cuelang/cue/discussions/748

myitcv commented 2 years ago

See to what extent this is/isn't related to https://github.com/cue-lang/cue/issues/1298