Repository: ogen-go/ogen
Pull request #652 (2022-11-01 20:00:57)

fix(gen): use correct filename in log messages


Patch:
diff --git a/gen/utils.go b/gen/utils.go
index 113111f6..a80a8f8d 100644
--- a/gen/utils.go
+++ b/gen/utils.go
@@ -77,6 +77,7 @@ func statusText(code int) string {
 
 type pos interface {
 	Position() (location.Position, bool)
+	File() location.File
 }
 
 func zapPosition(filename string, l pos) zap.Field {
@@ -91,9 +92,9 @@ func zapPosition(filename string, l pos) zap.Field {
 }
 
 func (g *Generator) zapPosition(l pos) zap.Field {
-	return zapPosition(g.opt.File.Name, l)
+	return zapPosition(l.File().Name, l)
 }
 
 func (g *schemaGen) zapPosition(l pos) zap.Field {
-	return zapPosition(g.filename, l)
+	return zapPosition(l.File().Name, l)
 }
diff --git a/internal/location/location.go b/internal/location/location.go
new file mode 100644
index 00000000..37581ff8
--- /dev/null
+++ b/internal/location/location.go
@@ -0,0 +1,19 @@
+package location
+
+// Pointer is a location of a value.
+type Pointer struct {
+	// Source is the File where the value is located.
+	Source File
+	// Locator stores the Position of a value.
+	Locator Locator
+}
+
+// File returns the File where the value is located.
+func (p Pointer) File() File {
+	return p.Source
+}
+
+// Position returns the position of the value if it is set.
+func (p Pointer) Position() (Position, bool) {
+	return p.Locator.Position()
+}
diff --git a/internal/location/locator.go b/internal/location/locator.go
new file mode 100644
index 00000000..38bb544e
--- /dev/null
+++ b/internal/location/locator.go
@@ -0,0 +1,75 @@
+package location
+
+import yaml "github.com/go-faster/yamlx"
+
+// Locatable is an interface for JSON value position store.
+type Locatable interface {
+	// SetPosition sets the position of the value.
+	SetPosition(Position)
+
+	// Position returns the position of the value if it is set.
+	Position() (Position, bool)
+}
+
+// Locator stores the Position of a JSON value.
+type Locator struct {
+	position Position
+	set      bool
+}
+
+// Pointer makes a Pointer from the Locator and given File.
+func (l Locator) Pointer(file File) Pointer {
+	return Pointer{
+		Source:  file,
+		Locator: l,
+	}
+}
+
+// SetPosition sets the position of the value.
+func (l *Locator) SetPosition(loc Position) {
+	l.position = loc
+	l.set = true
+}
+
+// Position returns the position of the value if it is set.
+func (l Locator) Position() (Position, bool) {
+	return l.position, l.set
+}
+
+// Key tries to find the child node using given key and returns its position.
+//
+// See Key method of Position.
+func (l Locator) Key(key string) (loc Locator) {
+	if l.set {
+		loc.SetPosition(l.position.Key(key))
+	}
+	return
+}
+
+// Field tries to find the child node using given key and returns its position.
+//
+// See Field method of Position.
+func (l Locator) Field(key string) (loc Locator) {
+	if l.set {
+		loc.SetPosition(l.position.Field(key))
+	}
+	return
+}
+
+// Index tries to find the child node using given index and returns its position.
+//
+// See Index method of Position.
+func (l Locator) Index(idx int) (loc Locator) {
+	if l.set {
+		loc.SetPosition(l.position.Index(idx))
+	}
+	return
+}
+
+// UnmarshalYAML implements yaml.Unmarshaler.
+func (l *Locator) UnmarshalYAML(n *yaml.Node) error {
+	var loc Position
+	loc.FromNode(n)
+	l.SetPosition(loc)
+	return nil
+}
diff --git a/internal/location/position.go b/internal/location/position.go
index c7848ae0..470b0ad1 100644
--- a/internal/location/position.go
+++ b/internal/location/position.go
@@ -124,67 +124,3 @@ func (p Position) WithFilename(filename string) string {
 	}
 	return filename + p.String()
 }
-
-// Locatable is an interface for JSON value position store.
-type Locatable interface {
-	// SetPosition sets the position of the value.
-	SetPosition(Position)
-
-	// Position returns the position of the value if it is set.
-	Position() (Position, bool)
-}
-
-// Locator stores the Position of a JSON value.
-type Locator struct {
-	position Position
-	set      bool
-}
-
-// SetPosition sets the position of the value.
-func (l *Locator) SetPosition(loc Position) {
-	l.position = loc
-	l.set = true
-}
-
-// Position returns the position of the value if it is set.
-func (l Locator) Position() (Position, bool) {
-	return l.position, l.set
-}
-
-// Key tries to find the child node using given key and returns its position.
-//
-// See Key method of Position.
-func (l Locator) Key(key string) (loc Locator) {
-	if l.set {
-		loc.SetPosition(l.position.Key(key))
-	}
-	return
-}
-
-// Field tries to find the child node using given key and returns its position.
-//
-// See Field method of Position.
-func (l Locator) Field(key string) (loc Locator) {
-	if l.set {
-		loc.SetPosition(l.position.Field(key))
-	}
-	return
-}
-
-// Index tries to find the child node using given index and returns its position.
-//
-// See Index method of Position.
-func (l Locator) Index(idx int) (loc Locator) {
-	if l.set {
-		loc.SetPosition(l.position.Index(idx))
-	}
-	return
-}
-
-// UnmarshalYAML implements yaml.Unmarshaler.
-func (l *Locator) UnmarshalYAML(n *yaml.Node) error {
-	var loc Position
-	loc.FromNode(n)
-	l.SetPosition(loc)
-	return nil
-}
diff --git a/jsonschema/parser.go b/jsonschema/parser.go
index adc8f009..34eacef1 100644
--- a/jsonschema/parser.go
+++ b/jsonschema/parser.go
@@ -64,7 +64,7 @@ func (p *Parser) parse(schema *RawSchema, ctx *jsonpointer.ResolveCtx) (_ *Schem
 		}()
 	}
 	return p.parse1(schema, ctx, func(s *Schema) *Schema {
-		return p.extendInfo(schema, s)
+		return p.extendInfo(schema, s, ctx.File())
 	})
 }
 
@@ -457,7 +457,7 @@ func (p *Parser) parseMany(schemas []*RawSchema, loc location.Locator, ctx *json
 	return result, nil
 }
 
-func (p *Parser) extendInfo(schema *RawSchema, s *Schema) *Schema {
+func (p *Parser) extendInfo(schema *RawSchema, s *Schema, file location.File) *Schema {
 	s.ContentEncoding = schema.ContentEncoding
 	s.ContentMediaType = schema.ContentMediaType
 	s.Summary = schema.Summary
@@ -485,6 +485,6 @@ func (p *Parser) extendInfo(schema *RawSchema, s *Schema) *Schema {
 		}
 	}
 
-	s.Locator = schema.Common.Locator
+	s.Pointer = schema.Common.Locator.Pointer(file)
 	return s
 }
diff --git a/jsonschema/resolve.go b/jsonschema/resolve.go
index 3fab9b1f..35f9e221 100644
--- a/jsonschema/resolve.go
+++ b/jsonschema/resolve.go
@@ -86,6 +86,6 @@ func (p *Parser) resolve(ref string, ctx *jsonpointer.ResolveCtx) (*Schema, erro
 	return p.parse1(raw, ctx, func(s *Schema) *Schema {
 		s.Ref = ref
 		p.refcache[key] = s
-		return p.extendInfo(raw, s)
+		return p.extendInfo(raw, s, ctx.File())
 	})
 }
diff --git a/jsonschema/schema.go b/jsonschema/schema.go
index 92858920..86f88dff 100644
--- a/jsonschema/schema.go
+++ b/jsonschema/schema.go
@@ -84,7 +84,7 @@ type Schema struct {
 	Default    any
 	DefaultSet bool
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // AddExample adds example for this Schema.
diff --git a/openapi/example.go b/openapi/example.go
index c1d12b5b..116b2453 100644
--- a/openapi/example.go
+++ b/openapi/example.go
@@ -14,5 +14,5 @@ type Example struct {
 	Value         jsonschema.Example
 	ExternalValue string
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
diff --git a/openapi/mediatype.go b/openapi/mediatype.go
index 0018b78f..a5301664 100644
--- a/openapi/mediatype.go
+++ b/openapi/mediatype.go
@@ -14,7 +14,7 @@ type MediaType struct {
 	Examples map[string]*Example
 	Encoding map[string]*Encoding
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // Encoding is Encoding Type Object.
@@ -25,5 +25,5 @@ type Encoding struct {
 	Explode       bool
 	AllowReserved bool
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
diff --git a/openapi/operation.go b/openapi/operation.go
index 9270f500..a2d52048 100644
--- a/openapi/operation.go
+++ b/openapi/operation.go
@@ -26,7 +26,7 @@ type Operation struct {
 	//  * 1XX, 2XX, 3XX, 4XX, 5XX
 	Responses map[string]*Response
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // RequestBody of an OpenAPI Operation.
@@ -36,7 +36,7 @@ type RequestBody struct {
 	Required    bool
 	Content     map[string]*MediaType
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // Header is an OpenAPI Header definition.
@@ -50,5 +50,5 @@ type Response struct {
 	Content     map[string]*MediaType
 	// Links map[string]*Link
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
diff --git a/openapi/parameter.go b/openapi/parameter.go
index e9a3edf8..e7483479 100644
--- a/openapi/parameter.go
+++ b/openapi/parameter.go
@@ -46,7 +46,7 @@ type Parameter struct {
 	Required      bool
 	AllowReserved bool
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // ParameterContent describes OpenAPI Parameter content field.
diff --git a/openapi/parser/parse_example.go b/openapi/parser/parse_example.go
index 4ed570c2..1e5c716d 100644
--- a/openapi/parser/parse_example.go
+++ b/openapi/parser/parse_example.go
@@ -27,6 +27,6 @@ func (p *parser) parseExample(e *ogen.Example, ctx *jsonpointer.ResolveCtx) (_ *
 		Description:   e.Description,
 		Value:         e.Value,
 		ExternalValue: e.ExternalValue,
-		Locator:       locator,
+		Pointer:       locator.Pointer(ctx.File()),
 	}, nil
 }
diff --git a/openapi/parser/parse_header.go b/openapi/parser/parse_header.go
index 155e86f0..802930be 100644
--- a/openapi/parser/parse_header.go
+++ b/openapi/parser/parse_header.go
@@ -80,7 +80,7 @@ func (p *parser) parseHeader(name string, header *ogen.Header, ctx *jsonpointer.
 		Style:       inferParamStyle(locatedIn, header.Style),
 		Explode:     inferParamExplode(locatedIn, header.Explode),
 		Required:    header.Required,
-		Locator:     locator,
+		Pointer:     locator.Pointer(ctx.File()),
 	}
 
 	// TODO: Validate content?
diff --git a/openapi/parser/parse_mediatype.go b/openapi/parser/parse_mediatype.go
index f20885c3..65ceec1d 100644
--- a/openapi/parser/parse_mediatype.go
+++ b/openapi/parser/parse_mediatype.go
@@ -112,7 +112,7 @@ func (p *parser) parseMediaType(ct string, m ogen.Media, ctx *jsonpointer.Resolv
 					Style:         inferParamStyle(openapi.LocationQuery, e.Style),
 					Explode:       inferParamExplode(openapi.LocationQuery, e.Explode),
 					AllowReserved: e.AllowReserved,
-					Locator:       locator,
+					Pointer:       locator.Pointer(ctx.File()),
 				}
 				encoding.Headers, err = p.parseHeaders(e.Headers, ctx)
 				if err != nil {
@@ -128,7 +128,7 @@ func (p *parser) parseMediaType(ct string, m ogen.Media, ctx *jsonpointer.Resolv
 					Explode:       encoding.Explode,
 					Required:      prop.Required,
 					AllowReserved: encoding.AllowReserved,
-					Locator:       encoding.Locator,
+					Pointer:       encoding.Pointer,
 				}, ctx.File()); err != nil {
 					return errors.Wrap(err, "param style")
 				}
@@ -177,6 +177,6 @@ func (p *parser) parseMediaType(ct string, m ogen.Media, ctx *jsonpointer.Resolv
 		Example:  json.RawMessage(m.Example),
 		Examples: examples,
 		Encoding: encodings,
-		Locator:  locator,
+		Pointer:  locator.Pointer(ctx.File()),
 	}, nil
 }
diff --git a/openapi/parser/parse_parameter.go b/openapi/parser/parse_parameter.go
index ec2b32f5..aa894181 100644
--- a/openapi/parser/parse_parameter.go
+++ b/openapi/parser/parse_parameter.go
@@ -169,7 +169,7 @@ func (p *parser) parseParameter(param *ogen.Parameter, ctx *jsonpointer.ResolveC
 		Explode:       inferParamExplode(locatedIn, param.Explode),
 		Required:      param.Required,
 		AllowReserved: param.AllowReserved,
-		Locator:       locator,
+		Pointer:       locator.Pointer(ctx.File()),
 	}
 
 	// TODO: Validate content?
@@ -252,7 +252,7 @@ func (p *parser) validateParamStyle(param *openapi.Parameter, file location.File
 		},
 	}
 	wrap := func(field string, err error) error {
-		return p.wrapField(field, file, param.Locator, err)
+		return p.wrapField(field, file, param.Pointer.Locator, err)
 	}
 
 	styles, ok := table[param.In]
@@ -275,7 +275,7 @@ func (p *parser) validateParamStyle(param *openapi.Parameter, file location.File
 		if s == nil {
 			return nil
 		}
-		locator := s.Locator
+		locator := s.Pointer.Locator
 
 		switch s.Type {
 		case jsonschema.String, jsonschema.Integer, jsonschema.Number, jsonschema.Boolean:
@@ -316,7 +316,7 @@ func (p *parser) validateParamStyle(param *openapi.Parameter, file location.File
 				continue
 			}
 			if err := check(s); err != nil {
-				return p.wrapLocation(file, s.Locator, err)
+				return p.wrapLocation(file, s.Pointer.Locator, err)
 			}
 		}
 		return nil
diff --git a/openapi/parser/parse_path_item.go b/openapi/parser/parse_path_item.go
index 1534c778..e29e952b 100644
--- a/openapi/parser/parse_path_item.go
+++ b/openapi/parser/parse_path_item.go
@@ -86,7 +86,7 @@ func (p *parser) parseOp(
 		Description: spec.Description,
 		Deprecated:  spec.Deprecated,
 		HTTPMethod:  httpMethod,
-		Locator:     locator,
+		Pointer:     locator.Pointer(ctx.File()),
 	}
 
 	opParams, err := p.parseParams(spec.Parameters, locator.Field("parameters"), ctx)
diff --git a/openapi/parser/parse_request_body.go b/openapi/parser/parse_request_body.go
index 1097bef7..fe610377 100644
--- a/openapi/parser/parse_request_body.go
+++ b/openapi/parser/parse_request_body.go
@@ -45,6 +45,6 @@ func (p *parser) parseRequestBody(body *ogen.RequestBody, ctx *jsonpointer.Resol
 		Description: body.Description,
 		Required:    body.Required,
 		Content:     content,
-		Locator:     locator,
+		Pointer:     locator.Pointer(ctx.File()),
 	}, nil
 }
diff --git a/openapi/parser/parse_response.go b/openapi/parser/parse_response.go
index 08756e33..edd355fc 100644
--- a/openapi/parser/parse_response.go
+++ b/openapi/parser/parse_response.go
@@ -70,7 +70,7 @@ func (p *parser) parseResponse(resp *ogen.Response, ctx *jsonpointer.ResolveCtx)
 		Description: resp.Description,
 		Headers:     headers,
 		Content:     content,
-		Locator:     locator,
+		Pointer:     locator.Pointer(ctx.File()),
 	}, nil
 }
 
diff --git a/openapi/parser/parse_security.go b/openapi/parser/parse_security.go
index 46bb5972..d0c27d49 100644
--- a/openapi/parser/parse_security.go
+++ b/openapi/parser/parse_security.go
@@ -158,7 +158,7 @@ func (p *parser) validateOAuthFlows(flows *ogen.OAuthFlows, file location.File)
 	return forEachFlow(flows, check)
 }
 
-func cloneOAuthFlows(flows ogen.OAuthFlows) (r openapi.OAuthFlows) {
+func cloneOAuthFlows(flows ogen.OAuthFlows, file location.File) (r openapi.OAuthFlows) {
 	cloneFlow := func(flow *ogen.OAuthFlow) *openapi.OAuthFlow {
 		if flow == nil {
 			return nil
@@ -168,7 +168,7 @@ func cloneOAuthFlows(flows ogen.OAuthFlows) (r openapi.OAuthFlows) {
 			TokenURL:         flow.TokenURL,
 			RefreshURL:       flow.RefreshURL,
 			Scopes:           maps.Clone(flow.Scopes),
-			Locator:          flow.Common.Locator,
+			Pointer:          flow.Common.Locator.Pointer(file),
 		}
 	}
 
@@ -177,7 +177,7 @@ func cloneOAuthFlows(flows ogen.OAuthFlows) (r openapi.OAuthFlows) {
 		Password:          cloneFlow(flows.Password),
 		ClientCredentials: cloneFlow(flows.ClientCredentials),
 		AuthorizationCode: cloneFlow(flows.AuthorizationCode),
-		Locator:           flows.Common.Locator,
+		Pointer:           flows.Common.Locator.Pointer(file),
 	}
 }
 
@@ -232,9 +232,9 @@ func (p *parser) parseSecurityRequirements(
 					In:               spec.In,
 					Scheme:           spec.Scheme,
 					BearerFormat:     spec.BearerFormat,
-					Flows:            cloneOAuthFlows(flows),
+					Flows:            cloneOAuthFlows(flows, ctx.File()),
 					OpenIDConnectURL: spec.OpenIDConnectURL,
-					Locator:          spec.Common.Locator,
+					Pointer:          spec.Common.Locator.Pointer(ctx.File()),
 				},
 			})
 		}
diff --git a/openapi/parser/parse_webhook.go b/openapi/parser/parse_webhook.go
index 5b186fe4..e38278c0 100644
--- a/openapi/parser/parse_webhook.go
+++ b/openapi/parser/parse_webhook.go
@@ -18,7 +18,7 @@ func (p *parser) parseWebhook(name string, item *ogen.PathItem, ctx *jsonpointer
 	return openapi.Webhook{
 		Name:       name,
 		Operations: pi,
-		Locator:    item.Common.Locator,
+		Pointer:    item.Common.Locator.Pointer(ctx.File()),
 	}, nil
 }
 
diff --git a/openapi/security.go b/openapi/security.go
index b9c7adf0..4630540b 100644
--- a/openapi/security.go
+++ b/openapi/security.go
@@ -20,7 +20,7 @@ type Security struct {
 	Flows            OAuthFlows
 	OpenIDConnectURL string
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // OAuthFlows allows configuration of the supported OAuth Flows.
@@ -30,7 +30,7 @@ type OAuthFlows struct {
 	ClientCredentials *OAuthFlow
 	AuthorizationCode *OAuthFlow
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
 
 // OAuthFlow is configuration details for a supported OAuth Flow.
@@ -40,5 +40,5 @@ type OAuthFlow struct {
 	RefreshURL       string
 	Scopes           map[string]string // name -> description
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }
diff --git a/openapi/webhook.go b/openapi/webhook.go
index c50f2c12..f12038db 100644
--- a/openapi/webhook.go
+++ b/openapi/webhook.go
@@ -9,5 +9,5 @@ type Webhook struct {
 	// Operations of the webhook's Path Item.
 	Operations []*Operation
 
-	location.Locator `json:"-" yaml:"-"`
+	location.Pointer `json:"-" yaml:"-"`
 }


Test patch:
diff --git a/jsonschema/parser_test.go b/jsonschema/parser_test.go
index ac96583e..5418a84d 100644
--- a/jsonschema/parser_test.go
+++ b/jsonschema/parser_test.go
@@ -384,7 +384,7 @@ func TestSchemaExtensions(t *testing.T) {
 			}
 			a.NoError(err)
 			// Zero locator to simplify comparison.
-			out.Locator = location.Locator{}
+			out.Pointer = location.Pointer{}
 			a.Equal(tt.expect, out)
 		})
 	}
diff --git a/jsonschema/resolve_test.go b/jsonschema/resolve_test.go
index a67dafbc..cce87a12 100644
--- a/jsonschema/resolve_test.go
+++ b/jsonschema/resolve_test.go
@@ -125,11 +125,11 @@ func TestExternalReference(t *testing.T) {
 }
 
 func zeroLocator(s *Schema) {
-	var zeroed location.Locator
+	var zeroed location.Pointer
 	if s == nil {
 		return
 	}
-	s.Locator = zeroed
+	s.Pointer = zeroed
 
 	zeroLocator(s.Item)
 	for _, p := range s.Properties {