github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/tables/jsonparser.go (about)

     1  /*
     2  Copyright 2017 Google Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package tables
    18  
    19  import (
    20  	"encoding/json"
    21  	"io/ioutil"
    22  )
    23  
    24  type Definitions struct {
    25  	IsLabelArg                      map[string]bool
    26  	LabelBlacklist                  map[string]bool
    27  	IsListArg                       map[string]bool
    28  	IsSortableListArg               map[string]bool
    29  	SortableBlacklist               map[string]bool
    30  	SortableWhitelist               map[string]bool
    31  	NamePriority                    map[string]int
    32  	StripLabelLeadingSlashes        bool
    33  	ShortenAbsoluteLabelsToRelative bool
    34  }
    35  
    36  // ParseJSONDefinitions reads and parses JSON table definitions from file.
    37  func ParseJSONDefinitions(file string) (Definitions, error) {
    38  	var definitions Definitions
    39  
    40  	data, err := ioutil.ReadFile(file)
    41  	if err != nil {
    42  		return definitions, err
    43  	}
    44  
    45  	err = json.Unmarshal(data, &definitions)
    46  	return definitions, err
    47  }
    48  
    49  // ParseAndUpdateJSONDefinitions reads definitions from file and merges or
    50  // overrides the values in memory.
    51  func ParseAndUpdateJSONDefinitions(file string, merge bool) error {
    52  	definitions, err := ParseJSONDefinitions(file)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	if merge {
    58  		MergeTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
    59  	} else {
    60  		OverrideTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
    61  	}
    62  	return nil
    63  }