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

     1  // Copyright 2016 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  	"fmt"
     9  
    10  	"github.com/open-policy-agent/opa/ast"
    11  )
    12  
    13  var errBadPath = fmt.Errorf("bad document path")
    14  
    15  func mergeTermWithValues(exist *ast.Term, pairs [][2]*ast.Term) (*ast.Term, error) {
    16  
    17  	var result *ast.Term
    18  	var init bool
    19  
    20  	for i, pair := range pairs {
    21  
    22  		if err := ast.IsValidImportPath(pair[0].Value); err != nil {
    23  			return nil, errBadPath
    24  		}
    25  
    26  		target := pair[0].Value.(ast.Ref)
    27  
    28  		// Copy the value if subsequent pairs in the slice would modify it.
    29  		for j := i + 1; j < len(pairs); j++ {
    30  			other := pairs[j][0].Value.(ast.Ref)
    31  			if len(other) > len(target) && other.HasPrefix(target) {
    32  				pair[1] = pair[1].Copy()
    33  				break
    34  			}
    35  		}
    36  
    37  		if len(target) == 1 {
    38  			result = pair[1]
    39  			init = true
    40  		} else {
    41  			if !init {
    42  				result = exist.Copy()
    43  				init = true
    44  			}
    45  			if result == nil {
    46  				result = ast.NewTerm(makeTree(target[1:], pair[1]))
    47  			} else {
    48  				node := result
    49  				done := false
    50  				for i := 1; i < len(target)-1 && !done; i++ {
    51  					obj, ok := node.Value.(ast.Object)
    52  					if !ok {
    53  						result = ast.NewTerm(makeTree(target[i:], pair[1]))
    54  						done = true
    55  						continue
    56  					}
    57  					if child := obj.Get(target[i]); !isObject(child) {
    58  						obj.Insert(target[i], ast.NewTerm(makeTree(target[i+1:], pair[1])))
    59  						done = true
    60  					} else { // child is object
    61  						node = child
    62  					}
    63  				}
    64  				if !done {
    65  					if obj, ok := node.Value.(ast.Object); ok {
    66  						obj.Insert(target[len(target)-1], pair[1])
    67  					} else {
    68  						result = ast.NewTerm(makeTree(target[len(target)-1:], pair[1]))
    69  					}
    70  				}
    71  			}
    72  		}
    73  	}
    74  
    75  	if !init {
    76  		result = exist
    77  	}
    78  
    79  	return result, nil
    80  }
    81  
    82  // makeTree returns an object that represents a document where the value v is
    83  // the leaf and elements in k represent intermediate objects.
    84  func makeTree(k ast.Ref, v *ast.Term) ast.Object {
    85  	var obj ast.Object
    86  	for i := len(k) - 1; i >= 1; i-- {
    87  		obj = ast.NewObject(ast.Item(k[i], v))
    88  		v = &ast.Term{Value: obj}
    89  	}
    90  	obj = ast.NewObject(ast.Item(k[0], v))
    91  	return obj
    92  }
    93  
    94  func isObject(x *ast.Term) bool {
    95  	if x == nil {
    96  		return false
    97  	}
    98  	_, ok := x.Value.(ast.Object)
    99  	return ok
   100  }