Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bketelsen committed Apr 12, 2021
1 parent 4335adc commit e455b25
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 228 deletions.
26 changes: 11 additions & 15 deletions cmd/blox_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ var buildCmd = &cobra.Command{
Short: "Validate and build your data",
Run: func(cmd *cobra.Command, args []string) {
// This can happen at a global Cobra level, if I knew how
config, err := cuedb.NewRuntime()
cobra.CheckErr(err)
cobra.CheckErr(config.LoadConfig())

runtime, err := cuedb.NewRuntime()
engine, err := cuedb.NewEngine()
cobra.CheckErr(err)

// Load Schemas!
schemaDir, err := config.GetString("schema_dir")
schemaDir, err := engine.Config.GetString("schema_dir")
cobra.CheckErr(err)

err = filepath.WalkDir(schemaDir, func(path string, d fs.DirEntry, err error) error {
Expand All @@ -47,7 +43,7 @@ var buildCmd = &cobra.Command{
return err
}

err = runtime.RegisterSchema(string(bb))
err = engine.RegisterSchema(string(bb))
if err != nil {
return err
}
Expand All @@ -56,25 +52,25 @@ var buildCmd = &cobra.Command{
})
cobra.CheckErr(err)

cobra.CheckErr(buildModels(&config, &runtime))
cobra.CheckErr(buildModels(engine))

if referentialIntegrity {
pterm.Info.Println("Checking Referential Integrity")
err = runtime.ReferentialIntegrity()
err = engine.ReferentialIntegrity()
if err != nil {
pterm.Error.Println(err)
} else {
pterm.Success.Println("Foreign Keys Validated")
}
}

output, err := runtime.GetOutput()
output, err := engine.GetOutput()
cobra.CheckErr(err)

jso, err := output.MarshalJSON()
cobra.CheckErr(err)

buildDir, err := config.GetString("build_dir")
buildDir, err := engine.Config.GetString("build_dir")
cobra.CheckErr(err)
err = os.MkdirAll(buildDir, 0755)
cobra.CheckErr(err)
Expand All @@ -86,15 +82,15 @@ var buildCmd = &cobra.Command{
},
}

func buildModels(config *cuedb.Runtime, runtime *cuedb.Runtime) error {
func buildModels(engine *cuedb.Engine) error {
var errors error

pterm.Info.Println("Validating ...")

for _, dataSet := range runtime.GetDataSets() {
for _, dataSet := range engine.GetDataSets() {
// We're using the Or variant of GetString because we know this call can't
// fail, as the config isn't valid without.
dataSetDirectory := fmt.Sprintf("%s/%s", config.GetStringOr("data_dir", ""), dataSet.GetDataDirectory())
dataSetDirectory := fmt.Sprintf("%s/%s", engine.Config.GetStringOr("data_dir", ""), dataSet.GetDataDirectory())

err := os.MkdirAll(dataSetDirectory, 0755)
if err != nil {
Expand Down Expand Up @@ -148,7 +144,7 @@ func buildModels(config *cuedb.Runtime, runtime *cuedb.Runtime) error {
record := make(map[string]interface{})
record[slug] = istruct

err = runtime.Insert(dataSet, record)
err = engine.Insert(dataSet, record)
if err != nil {
return multierror.Append(err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/remote_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ to quickly create a Cobra application.`,
}
}
}
config, err := cuedb.NewRuntime()
engine, err := cuedb.NewEngine()
cobra.CheckErr(err)
cobra.CheckErr(config.LoadConfig())

cobra.CheckErr(err)
schemaDir := config.GetStringOr("schema_dir", "schema")
schemaDir := engine.Config.GetStringOr("schema_dir", "schema")

err = os.MkdirAll(schemaDir, 0755)
cobra.CheckErr(err)
Expand Down
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ func (r *Config) LoadConfig(path string) error {
return err
}

return r.loadConfigString(string(cueConfig))
return r.LoadConfigString(string(cueConfig))
}

func (r *Config) loadConfigString(cueConfig string) error {
cueInstance, err := r.cueRuntime.Compile("", cueConfig)
func (r *Config) LoadConfigString(cueConfig string) error {
cueInstance, err := r.CueRuntime.Compile("", cueConfig)
if err != nil {
return err
}

cueValue := cueInstance.Value()

r.database = r.database.Unify(cueValue)
if err = r.database.Validate(); err != nil {
r.Database = r.Database.Unify(cueValue)
if err = r.Database.Validate(); err != nil {
return err
}

return nil
}
func (r *Config) GetString(key string) (string, error) {
keyValue := r.database.LookupPath(cue.ParsePath(key))
keyValue := r.Database.LookupPath(cue.ParsePath(key))

if keyValue.Exists() {
return keyValue.String()
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetString(t *testing.T) {
t.Fatal("Failed to create a NewRuntime, which should never happen")
}

err = runtime.loadConfigString(`{
err = runtime.LoadConfigString(`{
data_dir: "my-data-dir"
}`)
assert.Equal(t, nil, err)
Expand Down
55 changes: 0 additions & 55 deletions internal/cuedb/config.go

This file was deleted.

27 changes: 0 additions & 27 deletions internal/cuedb/config_test.go

This file was deleted.

68 changes: 68 additions & 0 deletions internal/cuedb/dataset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cuedb

import (
"fmt"

"cuelang.org/go/cue"
)

// Can't use schemaField, yet.
const SchemaMetadataCue = `{
_schema: {
namespace: string
name: string
}
}`

type SchemaMetadata struct {
Namespace string
Name string
}

// Can't use dataSetField, yet.
const DataSetMetadataCue = `{
_dataset: {
plural: string
supportedExtensions: [...string]
}
}`

type DataSetMetadata struct {
Plural string
SupportedExtensions []string
}

type DataSet struct {
name string
schemaMetadata SchemaMetadata
cuePath cue.Path
metadata DataSetMetadata
}

// AddDataMap
func (d *DataSet) GetDataMapCue() string {
return fmt.Sprintf(`{
%s: %s: _
%s: %s: [ID=string]: %s.%s & {id: (ID)}
}`,
d.GetInlinePath(), d.name,
dataPathRoot, d.metadata.Plural, d.cuePath.String(), d.name,
)
}
func (d *DataSet) CueDataPath() cue.Path {
return cue.ParsePath(fmt.Sprintf("%s.%s", dataPathRoot, d.metadata.Plural))
}

func (d *DataSet) IsSupportedExtension(ext string) bool {
for _, val := range d.metadata.SupportedExtensions {
if val == ext {
return true
}
}

return false
}

func (d *DataSet) GetSupportedExtensions() []string {
return d.metadata.SupportedExtensions
}
Loading

0 comments on commit e455b25

Please sign in to comment.