blob: 478e93e0535f79ebcf2dd845759510bb7dafdbb8 [file] [log] [blame]
// Copyright 2018 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graph
import (
"fmt"
"sort"
"go.starlark.net/starlark"
)
// Graph is a DAG of keyed nodes.
//
// It implements starlark.HasAttrs interface and have the following methods:
// * key(typ1: string, id2: string, typ2: string, id2: string, ...): Key.
type Graph struct {
KeySet
}
// String is a part of starlark.Value interface
func (g *Graph) String() string { return "graph" }
// Type is a part of starlark.Value interface.
func (g *Graph) Type() string { return "graph" }
// Freeze is a part of starlark.Value interface.
func (g *Graph) Freeze() {}
// Truth is a part of starlark.Value interface.
func (g *Graph) Truth() starlark.Bool { return starlark.True }
// Hash is a part of starlark.Value interface.
func (g *Graph) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable") }
// AttrNames is a part of starlark.HasAttrs interface.
func (g *Graph) AttrNames() []string {
names := make([]string, 0, len(graphAttrs))
for k := range graphAttrs {
names = append(names, k)
}
sort.Strings(names)
return names
}
// Attr is a part of starlark.HasAttrs interface.
func (g *Graph) Attr(name string) (starlark.Value, error) {
impl, ok := graphAttrs[name]
if !ok {
return nil, nil // per Attr(...) contract
}
return impl.BindReceiver(g), nil
}
//// Starlark bindings for individual graph methods.
var graphAttrs = map[string]*starlark.Builtin{
"key": starlark.NewBuiltin("key", func(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if len(kwargs) != 0 {
return nil, fmt.Errorf("graph.key: not expecting keyword arguments")
}
pairs := make([]string, len(args))
for idx, arg := range args {
str, ok := arg.(starlark.String)
if !ok {
return nil, fmt.Errorf("graph.key: all arguments must be strings, arg #%d was %s", idx, arg.Type())
}
pairs[idx] = str.GoString()
}
return b.Receiver().(*Graph).Key(pairs...)
}),
}