Skip to content

Commit 86c657f

Browse files
committed
fix(gen): improve test coverage, improve using more Filecoin miner types
matching types to Go and Rust original code in go-state-types and builtin-actors.
1 parent b3db46d commit 86c657f

File tree

14 files changed

+1154
-106
lines changed

14 files changed

+1154
-106
lines changed

lib/gen/go.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,30 @@ export function generateGo (schema, options = {}) {
267267
}
268268
} else if ('copy' in typeDefn) {
269269
const { fromType } = typeDefn.copy
270+
271+
// Check for type-level annotations
272+
/** @type { { [k in string]: string }[]} */
273+
let annotations = []
274+
// @ts-ignore - annotations exist in the parsed data even if not in type definition
275+
if (typeof typeDefn.copy.annotations === 'object' && typeof typeDefn.copy.annotations.type === 'object') {
276+
// @ts-ignore
277+
annotations = typeDefn.copy.annotations.type
278+
}
279+
270280
// Resolve fromType - it could be a primitive or custom type
271281
let resolvedType = fromType
272282

273283
// Check if it's a primitive IPLD type
274284
if (['Bool', 'Int', 'Float', 'String', 'Bytes', 'Link'].includes(fromType)) {
275-
resolvedType = getGoType([], fromType)
285+
resolvedType = getGoType(annotations, fromType)
276286
resolvedType = fixGoType(imports, resolvedType, false)
277287
}
278288

279-
typesrc += `type ${typeName} = ${resolvedType}\n\n`
289+
// Check if we should use type alias (=) or named type (default)
290+
const useAlias = annotations.find(a => 'gotypealias' in a)
291+
const typeDeclaration = useAlias ? '= ' : ''
292+
293+
typesrc += `type ${typeName} ${typeDeclaration}${resolvedType}\n\n`
280294
}
281295
}
282296

lib/gen/rust.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ export function generateRust (schema) {
166166
// Build serde attributes
167167
const serdeAttrs = []
168168

169+
// Check for @rustserde annotations
170+
for (const annotation of annotations) {
171+
if ('rustserde' in annotation) {
172+
serdeAttrs.push(annotation.rustserde)
173+
}
174+
}
175+
169176
// Determine the serialization name
170177
let serializationName = fieldName
171178
if (hasExplicitRename && explicitRenameValue) {
@@ -189,15 +196,21 @@ export function generateRust (schema) {
189196
if (isMapRepr) {
190197
serdeAttrs.push('skip_serializing_if = "Option::is_none"')
191198
} else if (isTupleRepr) {
192-
serdeAttrs.push('default')
199+
// Only add 'default' if it's not already present
200+
if (!serdeAttrs.includes('default')) {
201+
serdeAttrs.push('default')
202+
}
193203
}
194204
} else if (fieldDefn.optional) {
195205
// Just optional: Option<T>
196206
fieldType = `Option<${fieldType}>`
197207
if (isMapRepr) {
198208
serdeAttrs.push('skip_serializing_if = "Option::is_none"')
199209
} else if (isTupleRepr) {
200-
serdeAttrs.push('default')
210+
// Only add 'default' if it's not already present
211+
if (!serdeAttrs.includes('default')) {
212+
serdeAttrs.push('default')
213+
}
201214
}
202215
} else if (fieldDefn.nullable) {
203216
// Just nullable: Option<T> but always serialize
@@ -359,12 +372,25 @@ export function generateRust (schema) {
359372
}
360373
} else if ('copy' in typeDefn) {
361374
const { fromType } = typeDefn.copy
375+
376+
// Check for type-level annotations only when copying from primitive types
377+
/** @type { { [k in string]: string }[]} */
378+
let annotations = []
379+
if (['Bool', 'Int', 'Float', 'String', 'Bytes', 'Link'].includes(fromType)) {
380+
// @ts-ignore - annotations exist in the parsed data even if not in type definition
381+
if (typeof typeDefn.copy.annotations === 'object' && typeof typeDefn.copy.annotations.type === 'object') {
382+
// @ts-ignore
383+
// For copy types, only use @unsigned annotation, ignore @rusttype
384+
annotations = typeDefn.copy.annotations.type.filter(a => !('rusttype' in a))
385+
}
386+
}
387+
362388
// Resolve fromType - it could be a primitive or custom type
363389
let resolvedType = fromType
364390

365391
// Check if it's a primitive IPLD type
366392
if (['Bool', 'Int', 'Float', 'String', 'Bytes', 'Link'].includes(fromType)) {
367-
resolvedType = getRustType([], fromType)
393+
resolvedType = getRustType(annotations, fromType)
368394
resolvedType = fixRustType(imports, resolvedType, false)
369395
}
370396

lib/gen/typescript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export function generateTypeScript (schema) {
382382
typesrc += ` export const ${name}: ${typeName} = ${value}\n`
383383
}
384384

385-
typesrc += ' \n'
385+
typesrc += '\n'
386386
typesrc += ` export function is${typeName}(value: any): value is ${typeName} {\n`
387387

388388
if (isIntRepr) {

schema-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ export type UnionRepresentation_Inline = {
158158
discriminantTable: { [ k in HexString]: TypeName }
159159
}
160160
export type HexString = string
161-
export type UnionRepresentation_StringPrefix = {
161+
export type UnionRepresentation_StringPrefix = {
162162
prefixes: { [ k in KindString]: TypeName }
163163
}
164-
export type UnionRepresentation_BytesPrefix = {
164+
export type UnionRepresentation_BytesPrefix = {
165165
prefixes: { [ k in KindString]: TypeName }
166166
}
167167
export type TypeDefnStruct = {

test/fixtures/gen/copy-types.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ import (
5656
"github.com/ipfs/go-cid"
5757
)
5858

59-
type UserID = string
59+
type UserID string
6060

61-
type Age = int64
61+
type Age int64
6262

63-
type Balance = float64
63+
type Balance float64
6464

65-
type Data = []byte
65+
type Data []byte
6666

67-
type Reference = cid.Cid
67+
type Reference cid.Cid
6868

6969
type Person struct {
7070
name string
7171
age int64
7272
}
7373

74-
type Employee = Person
74+
type Employee Person
7575

7676
type Status string
7777

@@ -81,25 +81,25 @@ const (
8181
StatusPending Status = "Pending"
8282
)
8383

84-
type UserStatus = Status
84+
type UserStatus Status
8585

8686
type Names []string
8787

88-
type TeamMembers = Names
88+
type TeamMembers Names
8989

9090
type Settings map[string]string
9191

92-
type Configuration = Settings
92+
type Configuration Settings
9393

94-
type ID = string
94+
type ID string
9595

96-
type UserIdentifier = ID
96+
type UserIdentifier ID
9797

98-
type AdminID = UserIdentifier
98+
type AdminID UserIdentifier
9999

100-
type SpecialInt = int64
100+
type SpecialInt int64
101101

102-
type RegularInt = SpecialInt
102+
type RegularInt SpecialInt
103103
```
104104

105105
[testmark]:# (test/rust)
@@ -236,7 +236,7 @@ export namespace Status {
236236
export const Active: Status = "Active"
237237
export const Inactive: Status = "Inactive"
238238
export const Pending: Status = "Pending"
239-
239+
240240
export function isStatus(value: any): value is Status {
241241
return value === "Active" || value === "Inactive" || value === "Pending"
242242
}

test/fixtures/gen/enums.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This file tests enum type code generation for Go, Rust, and TypeScript.
88
```ipldsch
99
type Status enum {
1010
| Pending
11-
| Active
11+
| Active
1212
| Completed
1313
| Failed
1414
}
@@ -148,7 +148,7 @@ export namespace Status {
148148
export const Active: Status = "Active"
149149
export const Completed: Status = "Completed"
150150
export const Failed: Status = "Failed"
151-
151+
152152
export function isStatus(value: any): value is Status {
153153
return value === "Pending" || value === "Active" || value === "Completed" || value === "Failed"
154154
}
@@ -161,7 +161,7 @@ export namespace Color {
161161
export const Green: Color = "g"
162162
export const Blue: Color = "b"
163163
export const Yellow: Color = "Yellow"
164-
164+
165165
export function isColor(value: any): value is Color {
166166
return value === "r" || value === "g" || value === "b" || value === "Yellow"
167167
}
@@ -175,7 +175,7 @@ export namespace ErrorCode {
175175
export const Unauthorized: ErrorCode = 401
176176
export const NotFound: ErrorCode = 404
177177
export const ServerError: ErrorCode = 500
178-
178+
179179
export function isErrorCode(value: any): value is ErrorCode {
180180
return value === 0 || value === 400 || value === 401 || value === 404 || value === 500
181181
}
@@ -194,7 +194,7 @@ export namespace RegisteredSealProof {
194194
export const StackedDrg512MiBV1_1: RegisteredSealProof = 7
195195
export const StackedDrg32GiBV1_1: RegisteredSealProof = 8
196196
export const StackedDrg64GiBV1_1: RegisteredSealProof = 9
197-
197+
198198
export function isRegisteredSealProof(value: any): value is RegisteredSealProof {
199199
return value >= 0 && value <= 9 && Number.isInteger(value)
200200
}

0 commit comments

Comments
 (0)