feat: recognize generated schema types (SchemaTypeName + UnwrapSchema)#20
Open
mlwelles wants to merge 2 commits into
Open
feat: recognize generated schema types (SchemaTypeName + UnwrapSchema)#20mlwelles wants to merge 2 commits into
mlwelles wants to merge 2 commits into
Conversation
Add the Schema interface (SchemaTypeName), the UnwrapSchema reflection helper, and the DgraphMapper interface (record.go). The client unwraps schema-defining values at the mutation and query boundary so generated wrapper types route to their backing schema struct. Plain structs do not implement Schema and are unaffected — UnwrapSchema is identity for them.
There was a problem hiding this comment.
3 issues found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="record_test.go">
<violation number="1" location="record_test.go:73">
P2: Tests use a local `recordingClient` mock to simulate unwrapping instead of exercising real client methods, so regressions in the actual integration points (7 client mutation/query methods) may go undetected.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| seen []any | ||
| } | ||
|
|
||
| func (c *recordingClient) capture(obj any) any { |
There was a problem hiding this comment.
P2: Tests use a local recordingClient mock to simulate unwrapping instead of exercising real client methods, so regressions in the actual integration points (7 client mutation/query methods) may go undetected.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At record_test.go, line 73:
<comment>Tests use a local `recordingClient` mock to simulate unwrapping instead of exercising real client methods, so regressions in the actual integration points (7 client mutation/query methods) may go undetected.</comment>
<file context>
@@ -0,0 +1,117 @@
+ seen []any
+}
+
+func (c *recordingClient) capture(obj any) any {
+ obj = UnwrapSchema(obj)
+ c.seen = append(c.seen, obj)
</file context>
Author
There was a problem hiding this comment.
Fixed in 9bc4289. Added TestClientUnwrapsWrapperThroughRealMutation, which inserts a wrapper through the real client and reads it back via Get, so a mutation method dropping its UnwrapSchema call is caught — the prior tests exercised UnwrapSchema only through a local mock. (Issue identified by cubic.)
…Schema Addresses review feedback on generated-schema-type routing: - UnwrapSchema no longer panics on a typed nil pointer: it returns the value untouched instead of invoking Unwrap on a nil receiver that may dereference. - UnwrapSchema now finds a pointer-receiver Unwrap on a wrapper passed by value, by looking the method up on an addressable copy (a value's method set excludes pointer-receiver methods). Tests: - Add unit tests for the typed-nil-pointer and value-wrapper cases. - Add an integration test (TestClientUnwrapsWrapperThroughRealMutation) that inserts a wrapper through the real client and reads it back, so a mutation method dropping its UnwrapSchema call is caught — the prior tests exercised UnwrapSchema in isolation through a local mock and would not catch that. Docs: add a runnable ExampleSchema showing the wrapper/Unwrap pattern.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
A small mechanism that lets the client recognize generated schema/wrapper
types and route them to their backing schema struct. It is the integration
point that lets a code generator (e.g.
modusgraph-gen) emit wrapper types theclient understands, without the client depending on the generator.
Schemainterface —SchemaTypeName() string; generated schema structsimplement it.
UnwrapSchema(obj)— returns the schema record insideobj: identity for adirect
Schema, the result ofUnwrap()when that result is aSchema, andidentity for everything else.
UnwrapSchema,so a wrapper routes to its inner record while plain structs pass through
untouched.
Self-contained and additive: builds and tests green.
The problem it solves
Code generators want to hand the client a richer wrapper type (a fluent builder,
a domain object) rather than the bare schema struct. The client only knows how
to reflect over schema structs.
UnwrapSchemais the bridge: a wrapper exposesUnwrap()returning its backingSchema, and the client unwraps it before theexisting reflection pipeline runs. Plain structs implement neither
SchemanorUnwrap, so they are unaffected —UnwrapSchemais identity for them.Review round — changes since first push
Addresses the automated review (cubic):
UnwrapSchemainvokedUnwrap()on atyped nil pointer, which panics if the method dereferences the receiver. It
now returns a nil pointer untouched.
whose
Unwrap()has a pointer receiver was not unwrapped, because a value'smethod set excludes pointer-receiver methods.
UnwrapSchemanow looks themethod up on an addressable copy.
UnwrapSchemathrough a localrecordingClient, so a real mutation methoddropping its
UnwrapSchemacall would go undetected. Added an integrationtest that inserts a wrapper through the real client and reads it back.
Tests
TestClientUnwrapsWrapperThroughRealMutation— inserts a wrapper via the realclient, asserts the inner record persisted and the UID was written back.
Documentation
A runnable
ExampleSchemashowing the wrapper/Unwrappattern, alongside theexisting doc comments on
SchemaandUnwrapSchema.