mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
fix: continued moving everything to orderedmaps plus cleaned up most the tests
This commit is contained in:
@@ -13,9 +13,11 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const Example = "Example"
|
||||
const Examples = "Examples"
|
||||
const Schema = "Schema"
|
||||
const (
|
||||
Example = "Example"
|
||||
Examples = "Examples"
|
||||
Schema = "Schema"
|
||||
)
|
||||
|
||||
type MockType int
|
||||
|
||||
@@ -27,7 +29,7 @@ const (
|
||||
// MockGenerator is used to generate mocks for high-level mockable structs or *base.Schema pointers.
|
||||
// The mock generator will attempt to generate a mock from a struct using the following fields:
|
||||
// - Example: any type, this is the default example to use if no examples are present.
|
||||
// - Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
|
||||
// - Examples: *orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
|
||||
// - Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
|
||||
//
|
||||
// The mock generator will attempt to generate a mock from a *base.Schema pointer.
|
||||
@@ -61,7 +63,7 @@ func (mg *MockGenerator) SetPretty() {
|
||||
|
||||
// GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields:
|
||||
// Example: any type, this is the default example to use if no examples are present.
|
||||
// Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
|
||||
// Examples: *orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
|
||||
// Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
|
||||
// The name parameter is optional, if provided, the mock generator will attempt to find an example with the given name.
|
||||
// If no name is provided, the first example will be used.
|
||||
@@ -89,10 +91,21 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
|
||||
}
|
||||
|
||||
// if the value has an example, try and render it out as is.
|
||||
exampleValue := v.FieldByName(Example).Interface()
|
||||
if exampleValue != nil {
|
||||
// try and serialize the example value
|
||||
return mg.renderMock(exampleValue), nil
|
||||
f := v.FieldByName(Example)
|
||||
if !f.IsNil() {
|
||||
// Pointer/Interface Shenanigans
|
||||
ex := f.Interface()
|
||||
if y, ok := ex.(*yaml.Node); ok {
|
||||
if y != nil {
|
||||
ex = y
|
||||
} else {
|
||||
ex = nil
|
||||
}
|
||||
}
|
||||
if ex != nil {
|
||||
// try and serialize the example value
|
||||
return mg.renderMock(ex), nil
|
||||
}
|
||||
}
|
||||
|
||||
// if there is no example, but there are multi-examples.
|
||||
@@ -100,8 +113,8 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
|
||||
examplesValue := examples.Interface()
|
||||
if examplesValue != nil && !examples.IsNil() {
|
||||
|
||||
// cast examples to orderedmap.Map[string, any]
|
||||
examplesMap := examplesValue.(orderedmap.Map[string, *highbase.Example])
|
||||
// cast examples to *orderedmap.Map[string, *highbase.Example]
|
||||
examplesMap := examplesValue.(*orderedmap.Map[string, *highbase.Example])
|
||||
|
||||
// if the name is not empty, try and find the example by name
|
||||
for pair := orderedmap.First(examplesMap); pair != nil; pair = pair.Next() {
|
||||
@@ -157,6 +170,11 @@ func (mg *MockGenerator) renderMock(v any) []byte {
|
||||
|
||||
func (mg *MockGenerator) renderMockJSON(v any) []byte {
|
||||
var data []byte
|
||||
|
||||
if y, ok := v.(*yaml.Node); ok {
|
||||
_ = y.Decode(&v)
|
||||
}
|
||||
|
||||
// determine the type, render properly.
|
||||
switch reflect.ValueOf(v).Kind() {
|
||||
case reflect.Map, reflect.Slice, reflect.Array, reflect.Struct, reflect.Ptr:
|
||||
|
||||
Reference in New Issue
Block a user