Skip to content

Commit

Permalink
Add metadata modeldecoder and generator logic (#4154) (#4232)
Browse files Browse the repository at this point in the history
* Add metadata modeldecoder and generator logic

* Introduce modeldecoder models for metadata with nullable values

* Generate model decoding and validation functions

* Run generator logic on `make update`

Co-authored-by: Silvia Mitter <[email protected]>
  • Loading branch information
axw and simitt authored Sep 23, 2020
1 parent d7f066c commit 5cd6c6e
Show file tree
Hide file tree
Showing 25 changed files with 3,242 additions and 8 deletions.
2 changes: 1 addition & 1 deletion NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,7 @@ Contents of "LICENSE":

--------------------------------------------------------------------
Dependency: github.com/json-iterator/go
Version: v1.1.8
Version: v1.1.10
License type (autodetected): MIT
Contents of "LICENSE":

Expand Down
45 changes: 45 additions & 0 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 decoder

import (
"io"

jsoniter "github.com/json-iterator/go"
)

//TODO(simitt): look into config options for performance tuning
var jsonit = jsoniter.ConfigCompatibleWithStandardLibrary

type Decoder interface {
Decode(v interface{}) error
}

// JSONIterDecoder can decode from a given reader, using jsoniter
// TODO(simitt): rename to JSONDecoder when everything is integrated
type JSONIterDecoder struct {
*jsoniter.Decoder
}

// NewJSONIteratorDecoder returns a *json.Decoder where numbers are unmarshaled
// as a Number instead of a float64 into an interface{}
func NewJSONIteratorDecoder(r io.Reader) JSONIterDecoder {
d := jsonit.NewDecoder(r)
d.UseNumber()
return JSONIterDecoder{Decoder: d}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/jaegertracing/jaeger v1.16.0
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/josephspurrier/goversioninfo v1.2.0 // indirect
github.com/json-iterator/go v1.1.8
github.com/json-iterator/go v1.1.10
github.com/jstemmer/go-junit-report v0.9.1
github.com/klauspost/compress v1.9.3-0.20191122130757-c099ac9f21dd // indirect
github.com/kr/pretty v0.2.0 // indirect
Expand Down Expand Up @@ -75,7 +75,7 @@ require (
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/tools v0.0.0-20200823205832-c024452afbcd // indirect
golang.org/x/tools v0.0.0-20200823205832-c024452afbcd
google.golang.org/grpc v1.29.1
gopkg.in/yaml.v2 v2.3.0
howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46Ok=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package main

//go:generate go run script/inline_schemas/inline_schemas.go
//go:generate go run model/modeldecoder/generator/cmd/main.go

import (
"os"
Expand Down
87 changes: 87 additions & 0 deletions model/modeldecoder/generator/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 main

import (
"bytes"
"go/format"
"os"
"path"
"path/filepath"

"github.com/elastic/apm-server/model/modeldecoder/generator"
)

const (
basePath = "github.com/elastic/apm-server"
modeldecoderPath = "model/modeldecoder"
)

var (
importPath = path.Join(basePath, modeldecoderPath)
typPath = path.Join(importPath, "nullable")
)

func main() {
genV2Models()
genRUMV3Models()
}

func genV2Models() {
pkg := "v2"
rootObjs := []string{"metadataRoot"}
out := filepath.Join(filepath.FromSlash(modeldecoderPath), pkg, "model_generated.go")
gen, err := generator.NewGenerator(importPath, pkg, typPath, rootObjs)
if err != nil {
panic(err)
}
generate(gen, out)
}

func genRUMV3Models() {
pkg := "rumv3"
rootObjs := []string{"metadataRoot"}
out := filepath.Join(filepath.FromSlash(modeldecoderPath), pkg, "model_generated.go")
gen, err := generator.NewGenerator(importPath, pkg, typPath, rootObjs)
if err != nil {
panic(err)
}
generate(gen, out)
}

type gen interface {
Generate() (bytes.Buffer, error)
}

func generate(g gen, p string) {
b, err := g.Generate()
if err != nil {
panic(err)
}
fmtd, err := format.Source(b.Bytes())
if err != nil {
panic(err)
}
f, err := os.Create(p)
if err != nil {
panic(err)
}
if _, err := f.Write(fmtd); err != nil {
panic(err)
}
}
Loading

0 comments on commit 5cd6c6e

Please sign in to comment.