-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
25 changed files
with
3,242 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.