-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: removes viper for config loading and uses JSON standard library
Due to inconsistencies between the way viper and the JSON standard library unmarshals number values between float/int, removing in favor of sticking to the standard lib to have consistent attribute sets. Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
- Loading branch information
Showing
22 changed files
with
101 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,42 @@ | ||
package v1alpha1 | ||
|
||
// TypeMeta contains type metadata. | ||
type TypeMeta struct { | ||
Kind string `json:"kind,omitempty"` | ||
APIVersion string `json:"apiVersion,omitempty"` | ||
} | ||
|
||
// DataSetConfigurationKind object kind of DataSetConfiguration. | ||
const DataSetConfigurationKind = "DataSetConfiguration" | ||
|
||
// DataSetConfiguration configures a dataset | ||
type DataSetConfiguration struct { | ||
Kind string `mapstructure:"kind,omitempty"` | ||
APIVersion string `mapstructure:"apiVersion,omitempty"` | ||
TypeMeta `json:",inline"` | ||
// Collection configuration spec. | ||
Collection Collection `mapstructure:"collection,omitempty"` | ||
Collection Collection `json:"collection,omitempty"` | ||
// LinkedCollections are the remote addresses of collection that are | ||
// linked to the collection. | ||
LinkedCollections []string `mapstructure:"linkedCollections,omitempty"` | ||
LinkedCollections []string `json:"linkedCollections,omitempty"` | ||
} | ||
|
||
type Collection struct { | ||
// Files defines custom attributes to add the files in the | ||
// workspaces when publishing content/ | ||
Files []File `mapstructure:"files,omitempty"` | ||
Files []File `json:"files,omitempty"` | ||
// SchemaAddress is the address of the schema to associated | ||
// to the Collection. | ||
SchemaAddress string `mapstructure:"schemaAddress,omitempty"` | ||
SchemaAddress string `json:"schemaAddress,omitempty"` | ||
} | ||
|
||
// File associates attributes with file names. | ||
type File struct { | ||
// File is a string that can be compiled into a regular expression | ||
// for grouping attributes. | ||
File string `mapstructure:"file,omitempty"` | ||
File string `json:"file,omitempty"` | ||
// Attributes is the lists of to associate to the file. | ||
Attributes Attributes `mapstructure:"attributes,omitempty"` | ||
Attributes Attributes `json:"attributes,omitempty"` | ||
} | ||
|
||
// Attributes is a map structure that holds all | ||
// attribute information. | ||
type Attributes map[string]interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,72 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/spf13/viper" | ||
"path/filepath" | ||
|
||
"github.com/uor-framework/uor-client-go/builder/api/v1alpha1" | ||
"io/ioutil" | ||
"path/filepath" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// ReadCollectionConfig read the specified config into a CollectionConfiguration type. | ||
func ReadCollectionConfig(configPath string) (v1alpha1.DataSetConfiguration, error) { | ||
var configuration v1alpha1.DataSetConfiguration | ||
cfg, err := readInConfig(configPath, configuration) | ||
data, err := readInConfig(configPath, v1alpha1.DataSetConfigurationKind) | ||
if err != nil { | ||
return configuration, err | ||
} | ||
dsConfig := cfg.(v1alpha1.DataSetConfiguration) | ||
if dsConfig.Kind != v1alpha1.DataSetConfigurationKind { | ||
return v1alpha1.DataSetConfiguration{}, fmt.Errorf("config kind not recognized: %s", dsConfig.Kind) | ||
|
||
dec := json.NewDecoder(bytes.NewBuffer(data)) | ||
dec.DisallowUnknownFields() | ||
if err := dec.Decode(&configuration); err != nil { | ||
return configuration, err | ||
} | ||
return dsConfig, nil | ||
|
||
return configuration, nil | ||
} | ||
|
||
// ReadAttributeQuery read the specified config into a AttributeQuery type. | ||
func ReadAttributeQuery(configPath string) (v1alpha1.AttributeQuery, error) { | ||
var configuration v1alpha1.AttributeQuery | ||
cfg, err := readInConfig(configPath, configuration) | ||
data, err := readInConfig(configPath, v1alpha1.AttributeQueryKind) | ||
if err != nil { | ||
return configuration, err | ||
} | ||
|
||
queryConfig := cfg.(v1alpha1.AttributeQuery) | ||
if queryConfig.Kind != v1alpha1.AttributeQueryKind { | ||
return v1alpha1.AttributeQuery{}, fmt.Errorf("config kind not recognized: %s", queryConfig.Kind) | ||
dec := json.NewDecoder(bytes.NewBuffer(data)) | ||
dec.DisallowUnknownFields() | ||
if err := dec.Decode(&configuration); err != nil { | ||
return configuration, err | ||
} | ||
return queryConfig, nil | ||
} | ||
|
||
func readInConfig(configPath string, object interface{}) (interface{}, error) { | ||
base := filepath.Base(configPath) | ||
dir := filepath.Dir(configPath) | ||
viper.SetConfigName(base) | ||
viper.AddConfigPath(filepath.Clean(dir)) | ||
viper.SetConfigType("yaml") | ||
return configuration, nil | ||
} | ||
|
||
err := viper.ReadInConfig() | ||
func readInConfig(configPath, kind string) ([]byte, error) { | ||
data, err := ioutil.ReadFile(filepath.Clean(configPath)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = viper.Unmarshal(&object) | ||
|
||
if data, err = yaml.YAMLToJSON(data); err != nil { | ||
return nil, err | ||
} | ||
|
||
typeMeta, err := getTypeMeta(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return object, nil | ||
if typeMeta.Kind != kind { | ||
return nil, fmt.Errorf("config kind %s, does not match expected %s", typeMeta.Kind, kind) | ||
} | ||
return data, nil | ||
} | ||
|
||
func getTypeMeta(data []byte) (typeMeta v1alpha1.TypeMeta, err error) { | ||
if err := json.Unmarshal(data, &typeMeta); err != nil { | ||
return typeMeta, fmt.Errorf("get type meta: %v", err) | ||
} | ||
return typeMeta, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kind: AttributeQuery | ||
apiVersion: client.uor-framework.io/v1alpha1 | ||
attributes: | ||
- "test": "linkedannotation" | ||
"test": "linkedannotation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kind: AttributeQuery | ||
apiVersion: client.uor-framework.io/v1alpha1 | ||
attributes: | ||
- "test": "annotation" | ||
"test": "annotation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kind: AttributeQuery | ||
apiVersion: client.uor-framework.io/v1alpha1 | ||
attributes: | ||
- "test2": annotation" | ||
"test2": annotation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kind: AttributeQuery | ||
apiVersion: client.uor-framework.io/v1alpha1 | ||
attributes: | ||
size: "small" | ||
size: 2 |
Oops, something went wrong.