github.com/rajeev159/opa@v0.45.0/ast/schema.go (about)

     1  // Copyright 2021 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 ast
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/open-policy-agent/opa/types"
    11  	"github.com/open-policy-agent/opa/util"
    12  )
    13  
    14  // SchemaSet holds a map from a path to a schema.
    15  type SchemaSet struct {
    16  	m *util.HashMap
    17  }
    18  
    19  // NewSchemaSet returns an empty SchemaSet.
    20  func NewSchemaSet() *SchemaSet {
    21  
    22  	eqFunc := func(a, b util.T) bool {
    23  		return a.(Ref).Equal(b.(Ref))
    24  	}
    25  
    26  	hashFunc := func(x util.T) int { return x.(Ref).Hash() }
    27  
    28  	return &SchemaSet{
    29  		m: util.NewHashMap(eqFunc, hashFunc),
    30  	}
    31  }
    32  
    33  // Put inserts a raw schema into the set.
    34  func (ss *SchemaSet) Put(path Ref, raw interface{}) {
    35  	ss.m.Put(path, raw)
    36  }
    37  
    38  // Get returns the raw schema identified by the path.
    39  func (ss *SchemaSet) Get(path Ref) interface{} {
    40  	if ss == nil {
    41  		return nil
    42  	}
    43  	x, ok := ss.m.Get(path)
    44  	if !ok {
    45  		return nil
    46  	}
    47  	return x
    48  }
    49  
    50  func loadSchema(raw interface{}, allowNet []string) (types.Type, error) {
    51  
    52  	jsonSchema, err := compileSchema(raw, allowNet)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	tpe, err := parseSchema(jsonSchema.RootSchema)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("type checking: %w", err)
    60  	}
    61  
    62  	return tpe, nil
    63  }