Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] [pdata] Prototype immutable/mutable pdata #6798

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
const accessorSliceTemplate = `// ${fieldName} returns the ${fieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}

// ${fieldName} returns the ${fieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${packageName}Mutable${returnType} {
return ${packageName}Mutable${returnType}(internal.NewMutable${returnType}(&ms.getOrig().${fieldName}))
}`

const accessorsSliceTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand All @@ -35,6 +40,11 @@ const accessorsSliceTestTemplate = `func Test${structName}_${fieldName}(t *testi
const accessorsMessageValueTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}

// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${packageName}Mutable${returnType} {
return ${packageName}Mutable${returnType}(internal.NewMutable${returnType}(&ms.getOrig().${fieldName}))
}`

const accessorsMessageValueTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand All @@ -48,14 +58,24 @@ func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ms.getOrig().${fieldName}
}

// Mutable${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${packageName}${returnType} {
return ms.getOrig().${fieldName}
}

// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${returnType}) {
func (ms Mutable${structName}) Set${fieldName}(v ${returnType}) {
ms.getOrig().${fieldName} = v
}`

const accessorsPrimitiveSliceTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}

// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${packageName}Mutable${returnType} {
return ${packageName}Mutable${returnType}(internal.NewMutable${returnType}(&ms.getOrig().${fieldName}))
}`

const oneOfTypeAccessorHeaderTemplate = `// ${typeFuncName} returns the type of the ${lowerOriginFieldName} for this ${structName}.
Expand Down Expand Up @@ -87,11 +107,11 @@ func (ms ${structName}) ${fieldName}() ${returnType} {
//
// After this, ${originOneOfTypeFuncName}() function will return ${typeName}".
//
// Calling this function on zero-initialized ${structName} will cause a panic.
func (ms ${structName}) SetEmpty${fieldName}() ${returnType} {
// Calling this function on zero-initialized Mutable${structName} will cause a panic.
func (ms Mutable${structName}) SetEmpty${fieldName}() Mutable${returnType} {
val := &${originFieldPackageName}.${fieldName}{}
ms.getOrig().${originOneOfFieldName} = &${originStructType}{${fieldName}: val}
return new${returnType}(val)
return newMutable${returnType}(val)
}`

const accessorsOneOfMessageTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand All @@ -117,8 +137,12 @@ func (ms ${structName}) ${accessorFieldName}() ${returnType} {
return ms.getOrig().Get${originFieldName}()
}

func (ms Mutable${structName}) ${accessorFieldName}() ${returnType} {
return ms.getOrig().Get${originFieldName}()
}

// Set${accessorFieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${accessorFieldName}(v ${returnType}) {
func (ms Mutable${structName}) Set${accessorFieldName}(v ${returnType}) {
ms.getOrig().${originOneOfFieldName} = &${originStructType}{
${originFieldName}: v,
}
Expand All @@ -144,8 +168,13 @@ func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(ms.getOrig().${originFieldName})
}

// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(ms.getOrig().${originFieldName})
}

// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${packageName}${returnType}) {
func (ms Mutable${structName}) Set${fieldName}(v ${packageName}${returnType}) {
ms.getOrig().${originFieldName} = ${rawType}(v)
}`

Expand All @@ -169,19 +198,31 @@ func (ms ${structName}) ${fieldName}() ${returnType} {
return ms.getOrig().Get${fieldName}()
}

// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms Mutable${structName}) ${fieldName}() ${returnType} {
return ms.getOrig().Get${fieldName}()
}

// Has${fieldName} returns true if the ${structName} contains a
// ${fieldName} value, false otherwise.
func (ms ${structName}) Has${fieldName}() bool {
return ms.getOrig().${fieldName}_ != nil
}

// Has${fieldName} returns true if the ${structName} contains a
// ${fieldName} value, false otherwise.
func (ms Mutable${structName}) Has${fieldName}() bool {
return ms.getOrig().${fieldName}_ != nil
}


// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${returnType}) {
func (ms Mutable${structName}) Set${fieldName}(v ${returnType}) {
ms.getOrig().${fieldName}_ = &${originStructType}{${fieldName}: v}
}

// Remove${fieldName} removes the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Remove${fieldName}() {
func (ms Mutable${structName}) Remove${fieldName}() {
ms.getOrig().${fieldName}_ = nil
}`

Expand Down Expand Up @@ -251,7 +292,7 @@ func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) {
}

func (sf *sliceField) generateSetWithTestValue(sb *bytes.Buffer) {
sb.WriteString("\tFillTest" + sf.returnSlice.getName() + "(New" + sf.returnSlice.getName() + "(&tv.orig." + sf.fieldName + "))")
sb.WriteString("\tFillTest" + sf.returnSlice.getName() + "(NewMutable" + sf.returnSlice.getName() + "(&tv.orig." + sf.fieldName + "))")
}

func (sf *sliceField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) {
Expand Down Expand Up @@ -308,7 +349,7 @@ func (mf *messageValueField) generateAccessorsTest(ms baseStruct, sb *bytes.Buff
}

func (mf *messageValueField) generateSetWithTestValue(sb *bytes.Buffer) {
sb.WriteString("\tFillTest" + mf.returnMessage.getName() + "(New" + mf.returnMessage.getName() + "(&tv.orig." + mf.fieldName + "))")
sb.WriteString("\tFillTest" + mf.returnMessage.getName() + "(NewMutable" + mf.returnMessage.getName() + "(&tv.orig." + mf.fieldName + "))")
}

func (mf *messageValueField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) {
Expand Down Expand Up @@ -749,7 +790,7 @@ func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *b

func (omv *oneOfMessageValue) generateSetWithTestValue(of *oneOfField, sb *bytes.Buffer) {
sb.WriteString("\ttv.orig." + of.originFieldName + " = &" + of.originTypePrefix + omv.fieldName + "{" + omv.fieldName + ": &" + omv.originFieldPackageName + "." + omv.fieldName + "{}}\n")
sb.WriteString("\tFillTest" + omv.returnMessage.structName + "(New" + omv.fieldName + "(tv.orig.Get" + omv.fieldName + "()))")
sb.WriteString("\tFillTest" + omv.returnMessage.structName + "(NewMutable" + omv.fieldName + "(tv.orig.Get" + omv.fieldName + "()))")
}

func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *bytes.Buffer) {
Expand Down
Loading