go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/builtins/struct.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package builtins
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"go.starlark.net/starlark"
    21  	"go.starlark.net/starlarkstruct"
    22  )
    23  
    24  var (
    25  	// Struct is struct(**kwargs) builtin.
    26  	//
    27  	//  def struct(**kwargs):
    28  	//    """Returns an immutable object with fields set to given values."""
    29  	Struct = starlark.NewBuiltin("struct", starlarkstruct.Make)
    30  
    31  	// GenStruct is genstruct(name) builtin.
    32  	//
    33  	//  def genstruct(name):
    34  	//    """Returns a callable constructor for "branded" struct instances."""
    35  	GenStruct = starlark.NewBuiltin("genstruct", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    36  		var name string
    37  		if err := starlark.UnpackArgs("genstruct", args, kwargs, "name", &name); err != nil {
    38  			return nil, err
    39  		}
    40  		return &ctor{name: name}, nil
    41  	})
    42  
    43  	// Ctor is ctor(obj) builtin.
    44  	//
    45  	//  def ctor(obj):
    46  	//    """Returns a constructor used to construct this struct or None."""
    47  	Ctor = starlark.NewBuiltin("ctor", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    48  		var obj starlark.Value
    49  		if err := starlark.UnpackArgs("ctor", args, kwargs, "obj", &obj); err != nil {
    50  			return nil, err
    51  		}
    52  		if st, ok := obj.(*starlarkstruct.Struct); ok {
    53  			return st.Constructor(), nil
    54  		}
    55  		return starlark.None, nil
    56  	})
    57  )
    58  
    59  // Ctor is a callable that produces starlark structs that have it as a
    60  // constructor.
    61  type ctor struct{ name string }
    62  
    63  var _ starlark.Callable = (*ctor)(nil)
    64  
    65  func (c *ctor) Name() string          { return c.name }
    66  func (c *ctor) String() string        { return c.name }
    67  func (c *ctor) Type() string          { return "ctor" }
    68  func (c *ctor) Freeze()               {} // immutable
    69  func (c *ctor) Truth() starlark.Bool  { return starlark.True }
    70  func (c *ctor) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: ctor") }
    71  
    72  func (c *ctor) CallInternal(_ *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    73  	if len(args) > 0 {
    74  		return nil, fmt.Errorf("%s: unexpected positional arguments", c)
    75  	}
    76  	return starlarkstruct.FromKeywords(c, kwargs), nil
    77  }