Skip to content

Commit

Permalink
validate
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Dec 14, 2023
1 parent 27ea831 commit a098ba8
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 303 deletions.
138 changes: 73 additions & 65 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"oss.terrastruct.com/d2/d2ir"
"oss.terrastruct.com/d2/d2parser"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/lib/color"
"oss.terrastruct.com/d2/lib/textmeasure"
)

Expand Down Expand Up @@ -54,7 +55,11 @@ func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2ta
g.FS = opts.FS
g.SortObjectsByAST()
g.SortEdgesByAST()
return g, compileConfig(ir), nil
config, err := compileConfig(ir)
if err != nil {
return nil, nil, err
}
return g, config, nil
}

func compileIR(ast *d2ast.Map, m *d2ir.Map) (*d2graph.Graph, error) {
Expand Down Expand Up @@ -1336,10 +1341,10 @@ func parentSeqDiagram(n d2ir.Node) *d2ir.Map {
}
}

func compileConfig(ir *d2ir.Map) *d2target.Config {
func compileConfig(ir *d2ir.Map) (*d2target.Config, error) {
f := ir.GetField("vars", "d2-config")
if f == nil || f.Map() == nil {
return nil
return nil, nil
}

configMap := f.Map()
Expand Down Expand Up @@ -1377,82 +1382,85 @@ func compileConfig(ir *d2ir.Map) *d2target.Config {

f = configMap.GetField("theme-overrides")
if f != nil {
config.ThemeOverrides = compileThemeOverrides(f.Map())
overrides, err := compileThemeOverrides(f.Map())
if err != nil {
return nil, err
}
config.ThemeOverrides = overrides
}
f = configMap.GetField("dark-theme-overrides")
if f != nil {
config.DarkThemeOverrides = compileThemeOverrides(f.Map())
overrides, err := compileThemeOverrides(f.Map())
if err != nil {
return nil, err
}
config.DarkThemeOverrides = overrides
}

return config
return config, nil
}

func compileThemeOverrides(m *d2ir.Map) *d2target.ThemeOverrides {
func compileThemeOverrides(m *d2ir.Map) (*d2target.ThemeOverrides, error) {
if m == nil {
return nil
return nil, nil
}
themeOverrides := d2target.ThemeOverrides{}

if m.GetField("N1") != nil {
themeOverrides.N1 = go2.Pointer(m.GetField("N1").Primary().Value.ScalarString())
}
if m.GetField("B1") != nil {
themeOverrides.B1 = go2.Pointer(m.GetField("B1").Primary().Value.ScalarString())
}
if m.GetField("B2") != nil {
themeOverrides.B2 = go2.Pointer(m.GetField("B2").Primary().Value.ScalarString())
}
if m.GetField("B3") != nil {
themeOverrides.B3 = go2.Pointer(m.GetField("B3").Primary().Value.ScalarString())
}
if m.GetField("B4") != nil {
themeOverrides.B4 = go2.Pointer(m.GetField("B4").Primary().Value.ScalarString())
}
if m.GetField("B5") != nil {
themeOverrides.B5 = go2.Pointer(m.GetField("B5").Primary().Value.ScalarString())
}
if m.GetField("B6") != nil {
themeOverrides.B6 = go2.Pointer(m.GetField("B6").Primary().Value.ScalarString())
}
if m.GetField("AA2") != nil {
themeOverrides.AA2 = go2.Pointer(m.GetField("AA2").Primary().Value.ScalarString())
}
if m.GetField("AA4") != nil {
themeOverrides.AA4 = go2.Pointer(m.GetField("AA4").Primary().Value.ScalarString())
}
if m.GetField("AA5") != nil {
themeOverrides.AA5 = go2.Pointer(m.GetField("AA5").Primary().Value.ScalarString())
}
if m.GetField("AB4") != nil {
themeOverrides.AB4 = go2.Pointer(m.GetField("AB4").Primary().Value.ScalarString())
}
if m.GetField("AB5") != nil {
themeOverrides.AB5 = go2.Pointer(m.GetField("AB5").Primary().Value.ScalarString())
}
if m.GetField("N1") != nil {
themeOverrides.N1 = go2.Pointer(m.GetField("N1").Primary().Value.ScalarString())
}
if m.GetField("N2") != nil {
themeOverrides.N2 = go2.Pointer(m.GetField("N2").Primary().Value.ScalarString())
}
if m.GetField("N3") != nil {
themeOverrides.N3 = go2.Pointer(m.GetField("N3").Primary().Value.ScalarString())
}
if m.GetField("N4") != nil {
themeOverrides.N4 = go2.Pointer(m.GetField("N4").Primary().Value.ScalarString())
}
if m.GetField("N5") != nil {
themeOverrides.N5 = go2.Pointer(m.GetField("N5").Primary().Value.ScalarString())
}
if m.GetField("N6") != nil {
themeOverrides.N6 = go2.Pointer(m.GetField("N6").Primary().Value.ScalarString())
err := &d2parser.ParseError{}
FOR:
for _, f := range m.Fields {
switch strings.ToUpper(f.Name) {
case "N1":
themeOverrides.N1 = go2.Pointer(f.Primary().Value.ScalarString())
case "N2":
themeOverrides.N2 = go2.Pointer(f.Primary().Value.ScalarString())
case "N3":
themeOverrides.N3 = go2.Pointer(f.Primary().Value.ScalarString())
case "N4":
themeOverrides.N4 = go2.Pointer(f.Primary().Value.ScalarString())
case "N5":
themeOverrides.N5 = go2.Pointer(f.Primary().Value.ScalarString())
case "N6":
themeOverrides.N6 = go2.Pointer(f.Primary().Value.ScalarString())
case "N7":
themeOverrides.N7 = go2.Pointer(f.Primary().Value.ScalarString())
case "B1":
themeOverrides.B1 = go2.Pointer(f.Primary().Value.ScalarString())
case "B2":
themeOverrides.B2 = go2.Pointer(f.Primary().Value.ScalarString())
case "B3":
themeOverrides.B3 = go2.Pointer(f.Primary().Value.ScalarString())
case "B4":
themeOverrides.B4 = go2.Pointer(f.Primary().Value.ScalarString())
case "B5":
themeOverrides.B5 = go2.Pointer(f.Primary().Value.ScalarString())
case "B6":
themeOverrides.B6 = go2.Pointer(f.Primary().Value.ScalarString())
case "AA2":
themeOverrides.AA2 = go2.Pointer(f.Primary().Value.ScalarString())
case "AA4":
themeOverrides.AA4 = go2.Pointer(f.Primary().Value.ScalarString())
case "AA5":
themeOverrides.AA5 = go2.Pointer(f.Primary().Value.ScalarString())
case "AB4":
themeOverrides.AB4 = go2.Pointer(f.Primary().Value.ScalarString())
case "AB5":
themeOverrides.AB5 = go2.Pointer(f.Primary().Value.ScalarString())
default:
err.Errors = append(err.Errors, d2parser.Errorf(f.LastPrimaryKey(), fmt.Sprintf(`"%s" is not a valid theme code`, f.Name)).(d2ast.Error))
continue FOR
}
if !go2.Contains(color.NamedColors, strings.ToLower(f.Primary().Value.ScalarString())) && !color.ColorHexRegex.MatchString(f.Primary().Value.ScalarString()) {
err.Errors = append(err.Errors, d2parser.Errorf(f.LastPrimaryKey(), fmt.Sprintf(`expected "%s" to be a valid named color ("orange") or a hex code ("#f0ff3a")`, f.Name)).(d2ast.Error))
}
}
if m.GetField("N7") != nil {
themeOverrides.N7 = go2.Pointer(m.GetField("N7").Primary().Value.ScalarString())

if !err.Empty() {
return nil, err
}

if themeOverrides != (d2target.ThemeOverrides{}) {
return &themeOverrides
return &themeOverrides, nil
}
return nil
return nil, nil
}
15 changes: 15 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,21 @@ g: |md
d2/testdata/d2compiler/TestCompile/text_no_label.d2:15:1: block string cannot be empty
d2/testdata/d2compiler/TestCompile/text_no_label.d2:4:1: shape text must have a non-empty label
d2/testdata/d2compiler/TestCompile/text_no_label.d2:7:1: shape text must have a non-empty label`,
},
{
name: "var-not-color",
text: `vars: {
d2-config: {
theme-overrides: {
B1: potato
potato: B1
}
}
}
a
`,
expErr: `d2/testdata/d2compiler/TestCompile/var-not-color.d2:4:7: expected "B1" to be a valid named color ("orange") or a hex code ("#f0ff3a")
d2/testdata/d2compiler/TestCompile/var-not-color.d2:5:4: "potato" is not a valid theme code`,
},
{
name: "no_arrowheads_in_shape",
Expand Down
159 changes: 0 additions & 159 deletions d2graph/color_helper.go

This file was deleted.

6 changes: 3 additions & 3 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ func (s *Style) Apply(key, value string) error {
if s.Stroke == nil {
break
}
if !go2.Contains(namedColors, strings.ToLower(value)) && !colorHexRegex.MatchString(value) {
if !go2.Contains(color.NamedColors, strings.ToLower(value)) && !color.ColorHexRegex.MatchString(value) {
return errors.New(`expected "stroke" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
}
s.Stroke.Value = value
case "fill":
if s.Fill == nil {
break
}
if !go2.Contains(namedColors, strings.ToLower(value)) && !colorHexRegex.MatchString(value) {
if !go2.Contains(color.NamedColors, strings.ToLower(value)) && !color.ColorHexRegex.MatchString(value) {
return errors.New(`expected "fill" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
}
s.Fill.Value = value
Expand Down Expand Up @@ -347,7 +347,7 @@ func (s *Style) Apply(key, value string) error {
if s.FontColor == nil {
break
}
if !go2.Contains(namedColors, strings.ToLower(value)) && !colorHexRegex.MatchString(value) {
if !go2.Contains(color.NamedColors, strings.ToLower(value)) && !color.ColorHexRegex.MatchString(value) {
return errors.New(`expected "font-color" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
}
s.FontColor.Value = value
Expand Down
Loading

0 comments on commit a098ba8

Please sign in to comment.