Skip to content

Commit

Permalink
Move pdata into an internal package and use aliases in exported package
Browse files Browse the repository at this point in the history
This change is the first step towards splitting the pdata package into multiple modules
  • Loading branch information
dmitryax committed Mar 2, 2022
1 parent 4bde11b commit 41970be
Show file tree
Hide file tree
Showing 41 changed files with 805 additions and 10 deletions.
30 changes: 30 additions & 0 deletions model/internal/cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func fillTest${structName}(tv ${structName}) {
}
}`

const commonSliceAliasTemplate = `// ${structName} is an alias for pdata.${structName} struct.
type ${structName} = pdata.${structName}
// New${structName} is an alias for a function to create ${structName}.
var New${structName} = pdata.New${structName}`

const slicePtrTemplate = `// ${structName} logically represents a slice of ${elementName}.
//
// This is a reference type. If passed by value and callee modifies it, the
Expand Down Expand Up @@ -471,6 +477,18 @@ func (ss *sliceOfPtrs) templateFields() func(name string) string {
}
}

func (ss *sliceOfPtrs) generateAlias(sb *strings.Builder) {
sb.WriteString(os.Expand(commonSliceAliasTemplate, func(name string) string {
switch name {
case "structName":
return ss.structName
default:
panic(name)
}
}))
sb.WriteString(newLine + newLine)
}

var _ baseStruct = (*sliceOfPtrs)(nil)

// Will generate code only for a slice of value fields.
Expand Down Expand Up @@ -512,4 +530,16 @@ func (ss *sliceOfValues) templateFields() func(name string) string {
}
}

func (ss *sliceOfValues) generateAlias(sb *strings.Builder) {
sb.WriteString(os.Expand(commonSliceAliasTemplate, func(name string) string {
switch name {
case "structName":
return ss.structName
default:
panic(name)
}
}))
sb.WriteString(newLine + newLine)
}

var _ baseStruct = (*sliceOfValues)(nil)
22 changes: 22 additions & 0 deletions model/internal/cmd/pdatagen/internal/base_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const messageValueGenerateTestTemplate = `func generateTest${structName}() ${str
const messageValueFillTestHeaderTemplate = `func fillTest${structName}(tv ${structName}) {`
const messageValueFillTestFooterTemplate = `}`

const messageValueAliasTemplate = `// ${structName} is an alias for pdata.${structName} struct.
type ${structName} = pdata.${structName}
// New${structName} is an alias for a function to create a new empty ${structName}.
var New${structName} = pdata.New${structName}`

const newLine = "\n"

type baseStruct interface {
Expand All @@ -90,6 +96,10 @@ type baseStruct interface {
generateTestValueHelpers(sb *strings.Builder)
}

type aliasGenerator interface {
generateAlias(sb *strings.Builder)
}

type messageValueStruct struct {
structName string
description string
Expand Down Expand Up @@ -186,4 +196,16 @@ func (ms *messageValueStruct) generateTestValueHelpers(sb *strings.Builder) {
}))
}

func (ms *messageValueStruct) generateAlias(sb *strings.Builder) {
sb.WriteString(os.Expand(messageValueAliasTemplate, func(name string) string {
switch name {
case "structName":
return ms.structName
default:
panic(name)
}
}))
sb.WriteString(newLine + newLine)
}

var _ baseStruct = (*messageValueStruct)(nil)
21 changes: 21 additions & 0 deletions model/internal/cmd/pdatagen/internal/files.go

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

9 changes: 7 additions & 2 deletions model/internal/cmd/pdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ func check(e error) {

func main() {
for _, fp := range internal.AllFiles {
f, err := os.Create("./model/pdata/generated_" + fp.Name + ".go")
f, err := os.Create("./model/internal/pdata/generated_" + fp.Name + ".go")
check(err)
_, err = f.WriteString(fp.GenerateFile())
check(err)
check(f.Close())
f, err = os.Create("./model/pdata/generated_" + fp.Name + "_test.go")
f, err = os.Create("./model/internal/pdata/generated_" + fp.Name + "_test.go")
check(err)
_, err = f.WriteString(fp.GenerateTestFile())
check(err)
check(f.Close())
f, err = os.Create("./model/pdata/generated_" + fp.Name + "_alias.go")
check(err)
_, err = f.WriteString(fp.GenerateAliasFile())
check(err)
check(f.Close())
}
}
2 changes: 1 addition & 1 deletion model/pdata/common.go → model/internal/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

// This file contains data structures that are common for all telemetry types,
// such as timestamps, attributes, etc.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/doc.go → model/internal/pdata/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
// * Encoding: Common interfaces for serializing/deserializing bytes from/to protocol-specific data models.
// * Translation: Common interfaces for translating protocol-specific data models from/to pdata types.
// * Marshaling: Common higher level APIs that do both encoding and translation of bytes and data model if going directly pdata types to bytes.
package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/logs.go → model/internal/pdata/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"go.opentelemetry.io/collector/model/internal"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/metrics.go → model/internal/pdata/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"go.opentelemetry.io/collector/model/internal"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/spanid.go → model/internal/pdata/spanid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"go.opentelemetry.io/collector/model/internal/data"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"time"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/traceid.go → model/internal/pdata/traceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"go.opentelemetry.io/collector/model/internal/data"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion model/pdata/traces.go → model/internal/pdata/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"
package pdata // import "go.opentelemetry.io/collector/model/internal/pdata"

import (
"go.opentelemetry.io/collector/model/internal"
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions model/pdata/common_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pdata // import "go.opentelemetry.io/collector/model/pdata"

// This file contains aliases to data structures that are common for all
// signal types, such as timestamps, attributes, etc.

import "go.opentelemetry.io/collector/model/internal/pdata"

// AttributeValueType is an alias for pdata.AttributeValueType type.
type AttributeValueType = pdata.AttributeValueType

const (
AttributeValueTypeEmpty = pdata.AttributeValueTypeEmpty
AttributeValueTypeString = pdata.AttributeValueTypeString
AttributeValueTypeInt = pdata.AttributeValueTypeInt
AttributeValueTypeDouble = pdata.AttributeValueTypeDouble
AttributeValueTypeBool = pdata.AttributeValueTypeBool
AttributeValueTypeMap = pdata.AttributeValueTypeMap
AttributeValueTypeArray = pdata.AttributeValueTypeArray
AttributeValueTypeBytes = pdata.AttributeValueTypeBytes
)

// AttributeValue is an alias for pdata.AttributeValue struct.
type AttributeValue = pdata.AttributeValue

// Aliases for functions to create pdata.AttributeValue.
var (
NewAttributeValueEmpty = pdata.NewAttributeValueEmpty
NewAttributeValueString = pdata.NewAttributeValueString
NewAttributeValueInt = pdata.NewAttributeValueInt
NewAttributeValueDouble = pdata.NewAttributeValueDouble
NewAttributeValueBool = pdata.NewAttributeValueBool
NewAttributeValueMap = pdata.NewAttributeValueMap
NewAttributeValueArray = pdata.NewAttributeValueArray
NewAttributeValueBytes = pdata.NewAttributeValueBytes
)

// AttributeMap is an alias for pdata.AttributeMap struct.
type AttributeMap = pdata.AttributeMap

// Aliases for functions to create pdata.AttributeMap.
var (
NewAttributeMap = pdata.NewAttributeMap
NewAttributeMapFromMap = pdata.NewAttributeMapFromMap
)
32 changes: 32 additions & 0 deletions model/pdata/generated_common_alias.go

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

56 changes: 56 additions & 0 deletions model/pdata/generated_log_alias.go

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

Loading

0 comments on commit 41970be

Please sign in to comment.