Skip to content

Commit

Permalink
dev: embed direct xml into xml generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed Oct 10, 2024
1 parent 0c63082 commit ab1f5fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
16 changes: 16 additions & 0 deletions pkg/apis/clickhouse.altinity.com/v1/type_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Setting struct {
vector []string
src *SettingSource
attributes map[string]string
embed bool
}

type SettingType string
Expand Down Expand Up @@ -283,3 +284,18 @@ func (s *Setting) parseDataSourceAddress(dataSourceAddress, defaultNamespace str

return addr, nil
}

func (s *Setting) SetEmbed() *Setting {
if s == nil {
return nil
}
s.embed = true
return s
}

func (s *Setting) IsEmbed() bool {
if s == nil {
return false
}
return s.embed
}
2 changes: 1 addition & 1 deletion pkg/model/chk/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *Generator) getRaftConfig(selector *config.HostSelector) string {
return nil
})

return chi.NewSettings().Set("keeper_server/raft_configuration", chi.MustNewSettingScalarFromAny(raft)).ClickHouseConfig()
return chi.NewSettings().Set("keeper_server/raft_configuration", chi.MustNewSettingScalarFromAny(raft).SetEmbed()).ClickHouseConfig()
}

// getHostServerId builds server id config for the host
Expand Down
27 changes: 19 additions & 8 deletions pkg/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type setting interface {
IsVector() bool
Attributes() string
VectorOfStrings() []string
IsEmbed() bool
}

type settings interface {
Expand Down Expand Up @@ -156,13 +157,13 @@ func (n *xmlNode) buildXML(w io.Writer, indent, tabSize uint8) {

case n.value.IsScalar():
// ScalarString node
n.writeTagWithValue(w, n.value.String(), n.value.Attributes(), indent, tabSize)
n.writeTagWithValue(w, n.value.String(), n.value.Attributes(), indent, n.value.IsEmbed())
return

case n.value.IsVector():
// VectorOfStrings node
for _, value := range n.value.VectorOfStrings() {
n.writeTagWithValue(w, value, n.value.Attributes(), indent, tabSize)
n.writeTagWithValue(w, value, n.value.Attributes(), indent, n.value.IsEmbed())
}
}
}
Expand All @@ -186,19 +187,29 @@ func (n *xmlNode) writeTagNoValue(w io.Writer, attributes string, indent, tabSiz
// <tag>value</tag>
// OR
// <tag>
// long value NB - printed w/o indent
// embedded value NB - printed w/o indent
// </tag>
func (n *xmlNode) writeTagWithValue(w io.Writer, value string, attributes string, indent, tabSize uint8) {
func (n *xmlNode) writeTagWithValue(w io.Writer, value string, attributes string, indent uint8, embedded bool) {
// TODO fix this properly
// Used in tests
if value == "_removed_" || value == "_remove_" {
attributes = " remove=\"1\""
value = ""
}
// <tag>value</tag>
n.writeTagOpen(w, indent, attributes, noEol)
n.writeValue(w, value)
n.writeTagClose(w, 0, eol)

if embedded {
// <tag>
// embedded value NB - printed w/o indent
// </tag>
n.writeTagOpen(w, indent, attributes, eol)
n.writeValue(w, value)
n.writeTagClose(w, indent, eol)
} else {
// <tag>value</tag>
n.writeTagOpen(w, indent, attributes, noEol)
n.writeValue(w, value)
n.writeTagClose(w, 0, eol)
}
}

// writeTagOpen prints open XML tag into io.Writer
Expand Down

0 comments on commit ab1f5fb

Please sign in to comment.