github.com/rajeev159/opa@v0.45.0/topdown/uuid.go (about)

     1  // Copyright 2020 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package topdown
     6  
     7  import (
     8  	"github.com/open-policy-agent/opa/ast"
     9  	"github.com/open-policy-agent/opa/internal/uuid"
    10  )
    11  
    12  type uuidCachingKey string
    13  
    14  func builtinUUIDRFC4122(bctx BuiltinContext, args []*ast.Term, iter func(*ast.Term) error) error {
    15  
    16  	var key = uuidCachingKey(args[0].Value.String())
    17  
    18  	val, ok := bctx.Cache.Get(key)
    19  	if ok {
    20  		return iter(val.(*ast.Term))
    21  	}
    22  
    23  	s, err := uuid.New(bctx.Seed)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	result := ast.NewTerm(ast.String(s))
    29  	bctx.Cache.Put(key, result)
    30  
    31  	return iter(result)
    32  }
    33  
    34  func init() {
    35  	RegisterBuiltinFunc(ast.UUIDRFC4122.Name, builtinUUIDRFC4122)
    36  }