-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluator.go
58 lines (53 loc) · 1.47 KB
/
evaluator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"ariga.io/atlas/schemahcl"
"ariga.io/atlas/sql/mysql"
"ariga.io/atlas/sql/postgres"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlite"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/zclconf/go-cty/cty"
)
func toSchemaEvaluatorFunc(target string) (schemahcl.EvalFunc, error) {
switch target {
case "mysql":
return mysql.EvalHCL, nil
case "postgres", "postgresql":
return postgres.EvalHCL, nil
case "sqlite":
return sqlite.EvalHCL, nil
}
return nil, fmt.Errorf("unsupported target database: %s", target)
}
func toGoTypeString(ct *schema.ColumnType) string {
// TODO: support more types
// https://atlasgo.io/atlas-schema/hcl-types
switch ct.Type.(type) {
case *schema.IntegerType:
return "int"
case *schema.FloatType:
return "float"
case *schema.StringType:
return "string"
case *schema.BoolType:
return "bool"
case *schema.TimeType:
return "time.Time"
case *schema.EnumType:
return "string"
case *schema.BinaryType, *schema.JSONType:
return "[]byte"
}
return "any"
}
// original code: https://github.com/ariga/atlas/blob/98bb7b9da852536523121754d19570c506ba69f7/sql/internal/specutil/spec.go#L165...L173
func hclBytesFunc(ev schemahcl.Evaluator) func(b []byte, v any, inp map[string]cty.Value) error {
return func(b []byte, v any, inp map[string]cty.Value) error {
parser := hclparse.NewParser()
if _, diag := parser.ParseHCL(b, ""); diag.HasErrors() {
return diag
}
return ev.Eval(parser, v, inp)
}
}