SCGen Update February 7, 2019
Go's json.RawMessage helps big time in unstructured needs, which I put to heavy use in a new feature of my Sitecore Code Generator (which also does serialization and deserialization), SCGen.
With some client Sitecore work coming up, I've had to think about how to get rid of TDS for that specific client. I haven't had to do much Sitecore template work on that project at all, so i've been able to exclude TDS projects from the solution pretty much from the beginning.
However, the TDS code generation within that project created a Model.cs file that is nearly 53,000 lines. 2.4 MB!! It is pretty monumentally important that scgen can generate pretty much the same code, but without the enormous overhead of TDS. However, not the same code, as much of the code in that Model.cs is repeated, like full repeated "using" statements for every single type... (ugh). I can probably get it down to half the size, or even better!
The TDS code generator was generating things that weren't covered by scgen, like field wrapper properties along with field wrapper value getters. It was generating solr index attributes. Index field attributes would use the name of the property but as all lowercase and underscore separated, as well as the C# style property name.
The project Model.cs needed a lot of new things that just isn't covered by scgen, and I didn't want to add dedicated properties to scgen that only this project needs.
So, my solution...
Long story short, Go allows you to deserialize json to a concrete structure much like Newtonsoft.Json, and it allows a field type to be json.RawMessage. So the FieldType type looks like this now
type FieldType struct {
TypeName string `json:"typeName"`
CodeType string `json:"codeType"`
Suffix string `json:"suffix"`
PropertiesRaw json.RawMessage `json:"properties"`
Properties FieldTypePropertyMap
}
type FieldTypePropertyMap map[string]string
To define these properties, you can just add a "properties" json property to the field type. Here's an example for the "checkbox" field type:
{ "typeName": "Checkbox", "codeType": "bool", "suffix": "", "properties": {"SpecialProperty": "SpecialPropertyValue"} },
The process for including this and processing the text template is pretty verbose. It's not so bad if you can always depend on that property being there. But first, if you need to check for the property, you can use Go's Text Template "index" method, like so:
{{ if ne (index $field.Properties "propertyName") "" }} The property exists, use it here. {{ end }}
If the map doesn't include that property, it will just return a blank string. And to write it out in the template, simply do this, again with the index method:
{{ index $field.Properties "propertyName" }}
Feel free to browse the code at github.com/jasontconnell/scgen. There was also an update to the configuration helper that I use for nearly every project I create in Go, located at github.com/jasontconnell/conf.