1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39

infra / config / lib / dimensions.star [blame]

# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Utilities for working with dimensions."""

def _dimensions(**kwargs):
    """Get a dict that can be used where dimensions are expected.

    Args:
        **kwargs: The dimensions to be included. If the dimension value is a
            struct, it must have a get_dimension attribute which can be called
            with the bucket and builder as positional arguments to get the
            actual dimension value.

    Returns:
        A struct with a resolve attribute that can be called with the bucket and
        builder as positional arguments to get dict of dimension values.
    """

    def resolve(bucket, builder):
        def to_dimension(val):
            if type(val) == type(struct()):
                val = val.get_dimension(bucket, builder)
            if val == False:
                val = 0
            elif val == True:
                val = 1
            return str(val)

        return {k: to_dimension(v) for k, v in kwargs.items() if v != None}

    return struct(
        resolve = resolve,
    )

dimensions = struct(
    dimensions = _dimensions,
)