Skip to content

Commit

Permalink
feat(code-gen/go): introduce Copyable to clone messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Sep 26, 2024
1 parent 0402297 commit 919eca8
Show file tree
Hide file tree
Showing 2,485 changed files with 44,345 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,15 @@ public int getNumBits(SimpleTypeReference simpleTypeReference) {

public boolean needsPointerAccess(PropertyField field) {
boolean isAnTypeOfOptional = "optional".equals(field.getTypeName());
return isAnTypeOfOptional && needPointerAccess(field.getType());
return isAnTypeOfOptional && isNonComplexOrArrayElementNonComplex(field.getType());
}

public boolean needPointerAccess(TypeReference typeReference) {
public boolean isNonComplexOrArrayElementNonComplex(TypeReference typeReference) {
boolean isNotAnComplexTypeReference = !typeReference.isComplexTypeReference();
boolean arrayTypeIsNotAnComplexTypeReference = !(typeReference.isArrayTypeReference() && typeReference.asArrayTypeReference().orElseThrow().getElementTypeReference().isComplexTypeReference());
boolean arrayTypeIsNotAnComplexTypeReference = !(
typeReference.isArrayTypeReference() &&
typeReference.asArrayTypeReference().orElseThrow().getElementTypeReference().isComplexTypeReference()
);
return isNotAnComplexTypeReference && arrayTypeIsNotAnComplexTypeReference;
}

Expand Down Expand Up @@ -1100,7 +1103,7 @@ else if ((variableLiteral.getChild().isPresent()) && ((ComplexTypeDefinition) th
} else if ((serializerArguments != null) && serializerArguments.stream()
.anyMatch(argument -> argument.getName().equals(variableLiteralName))) {
tracer = tracer.dive("serialization argument");
return tracer + "m.Get" + capitalize(variableLiteralName) +"()"+
return tracer + "m.Get" + capitalize(variableLiteralName) + "()" +
variableLiteral.getChild()
.map(child -> "." + capitalize(toVariableExpression(field, typeReference, child, parserArguments, serializerArguments, false, suppressPointerAccess, true)))
.orElse("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ type ${type.name} interface {
fmt.Stringer<@emitImport import="fmt" />
utils.LengthAware
utils.Serializable
utils.Copyable
<#else>
fmt.Stringer<@emitImport import="fmt" />
utils.LengthAware
utils.Serializable
utils.Copyable
<#if type.isDiscriminatedChildTypeDefinition()>
${type.parentType.orElseThrow().name?cap_first}
</#if>
Expand Down Expand Up @@ -126,6 +128,7 @@ type ${type.name} interface {
<#if type.isDiscriminatedParentTypeDefinition()>
// ${type.name}Contract provides a set of functions which can be overwritten by a sub struct
type ${type.name}Contract interface {
utils.Copyable
<#if type.isDiscriminatedChildTypeDefinition()>
${type.parentType.orElseThrow().name?cap_first}<#if type.isDiscriminatedParentTypeDefinition()>Contract</#if>
</#if>
Expand Down Expand Up @@ -1616,7 +1619,54 @@ func (m *_${type.name}) Get${parserArgument.name?cap_first}() ${helper.getLangua
////
</#if>

func (m *_${type.name}) Is${type.name}() {}
func (m *_${type.name}) Is${type.name}() {}

func (m *_${type.name}) DeepCopy() any {
return m.deepCopy()
}

func (m *_${type.name}) deepCopy() *_${type.name} {
if m==nil{
return nil
}
_${type.name}Copy := &_${type.name} {
<#if type.parentType.isPresent()>
m.${type.parentType.orElseThrow().name}Contract.DeepCopy().(${type.parentType.orElseThrow().name}Contract),
</#if>
<#if type.isDiscriminatedParentTypeDefinition()>
nil, // will be set by child
</#if>
<#-- Property fields are fields that require a property in the pojo -->
<#if type.propertyFields?has_content>
<#list type.propertyFields as field>
<#if field.type.isComplexTypeReference()>
m.${field.name?cap_first}.DeepCopy().(<#if helper.needsPointerAccess(field)>*</#if>${helper.getLanguageTypeNameForTypeReference(field.type)}),
<#elseif field.type.isArrayTypeReference()>
<#assign arrayElementTypeRef=field.type.asArrayTypeReference().orElseThrow().elementTypeReference/>
utils.DeepCopySlice[${helper.getLanguageTypeNameForTypeReference(arrayElementTypeRef)}, ${helper.getLanguageTypeNameForTypeReference(arrayElementTypeRef)}](m.${field.name?cap_first}),
<#else>
<#if helper.needsPointerAccess(field)>utils.CopyPtr[${helper.getLanguageTypeNameForField(field)}](</#if>m.${field.name?cap_first}<#if helper.needsPointerAccess(field)>)</#if>,
</#if>
</#list>
</#if>
<#if filteredParserArgumentsLocalToType?has_content>
<#list filteredParserArgumentsLocalToType as parserArgument>
m.${parserArgument.name?cap_first},
</#list>
</#if>
<#assign reservedFields=type.getFields()?filter(f->f.isReservedField())>
<#if reservedFields?has_content>
<#list reservedFields as reservedField>
m.reservedField${reservedField?index},
</#list>
</#if>
}
<#if type.isDiscriminatedChildTypeDefinition()>
m.${type.parentType.orElseThrow().name}Contract.(*_${type.parentType.orElseThrow().name})._SubType = m
</#if>
return _${type.name}Copy
}


<#if !type.isDiscriminatedParentTypeDefinition()>
func (m *_${type.name}) String() string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions plc4go/protocols/abeth/readwrite/model/CIPEncapsulationPacket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions plc4go/protocols/abeth/readwrite/model/DF1CommandRequestMessage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions plc4go/protocols/abeth/readwrite/model/DF1RequestCommand.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions plc4go/protocols/abeth/readwrite/model/DF1RequestMessage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 919eca8

Please sign in to comment.