github.com/openconfig/goyang@v1.4.5/pkg/yangentry/build_yang.go (about)

     1  // Copyright 2020 Google Inc.
     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 yangentry contains high-level helpers for using yang.Entry objects.
    16  package yangentry
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/openconfig/goyang/pkg/yang"
    22  )
    23  
    24  // Parse takes a list of either module/submodule names or .yang file
    25  // paths, and a list of include paths. It runs the yang parser on the YANG
    26  // files by searching for them in the include paths or in the current
    27  // directory, returning a slice of yang.Entry pointers which represent the
    28  // parsed top level modules. It also returns a list of errors encountered while
    29  // parsing, if any.
    30  func Parse(yangfiles, path []string) (map[string]*yang.Entry, []error) {
    31  	ms := yang.NewModules()
    32  
    33  	for _, p := range path {
    34  		ms.AddPath(fmt.Sprintf("%s/...", p))
    35  	}
    36  
    37  	var processErr []error
    38  	for _, name := range yangfiles {
    39  		if name == "" {
    40  			continue
    41  		}
    42  		if err := ms.Read(name); err != nil {
    43  			processErr = append(processErr, err)
    44  		}
    45  	}
    46  
    47  	if len(processErr) > 0 {
    48  		return nil, processErr
    49  	}
    50  
    51  	if errs := ms.Process(); len(errs) != 0 {
    52  		return nil, errs
    53  	}
    54  
    55  	entries := make(map[string]*yang.Entry)
    56  	for _, m := range ms.Modules {
    57  		e := yang.ToEntry(m)
    58  		entries[e.Name] = e
    59  	}
    60  
    61  	return entries, nil
    62  }