Skip to content

Commit

Permalink
feat: add cue loader and experimental realm decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcec0de committed Nov 17, 2022
1 parent d3f0335 commit d3b3e52
Show file tree
Hide file tree
Showing 40 changed files with 2,518 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
104 changes: 104 additions & 0 deletions cue.mod/cudmod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cuemod

import (
"embed"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)

//go:embed gen module.cue
var cueModFS embed.FS

// Copy copies the contents of the embedded cue.mod directory to the given path.
func Copy(target string) (err error) {
// ensurer directory is a cue.mod directory
// maybe rethink this...
if !strings.HasSuffix(target, "/cue.mod") {
target = filepath.Join(target, "cue.mod")
}

if err = os.MkdirAll(target, 0755); err != nil {
return err
}

matches, err := fs.Glob(cueModFS, "./*")
if err != nil {
return
}

for _, file := range matches {
var stat fs.FileInfo
stat, err = fs.Stat(cueModFS, file)
if err != nil {
return
}

if stat.IsDir() {
err = copyDir(copyDirParams{
fsRoot: file,
targetDir: target,
})
} else {
err = copyFile(copyFileParams{
embeddedFile: file,
targetDir: target,
})
}

if err != nil {
return
}
}

return err
}

type copyDirParams struct {
fsRoot string
targetDir string
}

func copyDir(params copyDirParams) error {
return fs.WalkDir(cueModFS, params.fsRoot, func(path string, d fs.DirEntry, _ error) (err error) {
if d.IsDir() {
return os.MkdirAll(filepath.Join(params.targetDir, path), 0755)
}

err = copyFile(copyFileParams{
embeddedFile: path,
targetDir: params.targetDir,
})
if err != nil {
return
}
return nil
})
}

type copyFileParams struct {
targetDir string
embeddedFile string
}

func copyFile(params copyFileParams) (err error) {
vf, err := cueModFS.Open(params.embeddedFile)
if err != nil {
return err
}

dst := filepath.Join(params.targetDir, params.embeddedFile)
f, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755)
if err != nil {
return err
}
defer f.Close()

_, err = io.Copy(f, vf)
if err != nil {
return err
}
return nil
}
20 changes: 20 additions & 0 deletions cue.mod/cudmod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cuemod

import (
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
)

func TestCopy(t *testing.T) {
target, err := os.MkdirTemp("", "atlas")
require.NoError(t, err)

defer os.RemoveAll(target)

err = Copy(target)
require.NoError(t, err)
require.FileExists(t, filepath.Join(target, "cue.mod/module.cue"))
require.FileExists(t, filepath.Join(target, "cue.mod/gen/ariga.io/atlas/sql/schema/schema_go_gen.cue"))
}
19 changes: 19 additions & 0 deletions cue.mod/gen/ariga.io/atlas/schemahcl/context_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/schemahcl

package schemahcl

// blockVar is an HCL resource that defines an input variable to the Atlas DDL document.
_#blockVar: {
Name: string
Description: string
}

_#varBlock: "variable"
_#dataBlock: "data"
_#localsBlock: "locals"
_#forEachAttr: "for_each"
_#eachRef: "each"
_#varRef: "var"
_#localRef: "local"
25 changes: 25 additions & 0 deletions cue.mod/gen/ariga.io/atlas/schemahcl/extension_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/schemahcl

package schemahcl

import "reflect"

// Remainer is the interface that is implemented by types that can store
// additional attributes and children resources.
#Remainer: _

// DefaultExtension can be embedded in structs that need basic default behavior.
// For instance, DefaultExtension implements Remainer, and has a private *Resource
// field that can store additional attributes and children that do not match the
// structs fields.
#DefaultExtension: {
Extra: #Resource
}

_#registry: [string]: _

_#fieldDesc: StructField: reflect.#StructField

_#rtype: _
12 changes: 12 additions & 0 deletions cue.mod/gen/ariga.io/atlas/schemahcl/hcl_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/schemahcl

package schemahcl

#Evaluator: _

#Marshaler: _

// addrRef maps addresses to their referenced resource.
_#addrRef: {[string]: null | #Resource}
48 changes: 48 additions & 0 deletions cue.mod/gen/ariga.io/atlas/schemahcl/spec_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/schemahcl

package schemahcl

import "reflect"

#Resource: {
Name: string
Qualifier: string
Type: string
Attrs: [...null | #Attr] @go(,[]*Attr)
Children: [...null | #Resource] @go(,[]*Resource)
}

#Attr: K: string

#Ref: V: string

#RawExpr: X: string

#TypeSpec: {
// Name is the identifier for the type in an Atlas DDL document.
Name: string

// T is the database identifier for the type.
T: string
Attributes: [...null | #TypeAttr] @go(,[]*TypeAttr)

// RType is the reflect.Type of the schema.Type used to describe the TypeSpec.
// This field is optional and used to determine the TypeSpec in cases where the
// schema.Type does not have a `T` field.
RType: reflect.#Type
}

#TypeAttr: {
// Name should be a snake_case of related the schema.Type struct field.
Name: string
Kind: reflect.#Kind
Required: bool
}

#Type: {
T: string
Attrs: [...null | #Attr] @go(,[]*Attr)
IsRef: bool
}
24 changes: 24 additions & 0 deletions cue.mod/gen/ariga.io/atlas/sql/migrate/dir_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/sql/migrate

package migrate

#Dir: _

#Formatter: _

#File: _

#HashFileName: "atlas.sum"

// HashFile represents the integrity sum file of the migration dir.
#HashFile: string

// atlas:sum directive.
_#directiveSum: "sum"
_#sumModeIgnore: "ignore"

// atlas:delimiter directive.
_#directiveDelimiter: "delimiter"
_#directivePrefixSQL: "-- "
17 changes: 17 additions & 0 deletions cue.mod/gen/ariga.io/atlas/sql/migrate/lex_go_gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go ariga.io/atlas/sql/migrate

package migrate

// Stmt represents a scanned statement text along with its
// position in the file and associated comments group.
#Stmt: {
Pos: int
Text: string
Comments: [...string] @go(,[]string)
}

_#eos: -1
_#delimiter: ";"
_#delimiterCmd: "delimiter"
Loading

0 comments on commit d3b3e52

Please sign in to comment.