Always include complete imports at the top of examples:
1
2
3
4
5
6
7
8
9
10
11
12
// TrustWeave importsimportorg.trustweave.TrustWeaveimportorg.trustweave.core.*importorg.trustweave.did.*importorg.trustweave.credential.models.VerifiableCredential// Kotlinx importsimportkotlinx.coroutines.runBlocking// Note: buildJsonObject is only needed for standalone JSON creation outside DSL// Within DSL blocks (subject {}, credential {}, etc.), use DSL syntax insteadimportkotlinx.serialization.json.buildJsonObjectimportkotlinx.serialization.json.put
Import Organization
TrustWeave imports (grouped by module)
Kotlinx imports
Standard library imports
Third-party imports
DSL vs buildJsonObject
Within DSL blocks, use the DSL syntax:
1
2
3
4
5
6
7
8
9
10
11
12
subject{id("did:key:holder")"name"to"Alice""address"{"street"to"123 Main St""city"to"New York"}"grades"toarrayOfObjects({"course"to"CS101";"grade"to"A"},{"course"to"MATH101";"grade"to"B"})}
Outside DSL blocks (for standalone JSON creation), use buildJsonObject:
// Good: Explains why// Using getOrThrow() for quick start - in production use fold()valdid=TrustWeave.createDid().getOrThrow()// Good: Clarifies behavior// This creates an in-memory wallet (testing only)valwallet=TrustWeave.createWallet(holderDid).getOrThrow()// Bad: States the obvious// Create a DIDvaldid=TrustWeave.createDid().getOrThrow()
Variable Naming
Naming Conventions
Use descriptive names: issuerDid, credentialSubject, walletId
packagecom.example.TrustWeave.example// TrustWeave importsimportorg.trustweave.TrustWeaveimportorg.trustweave.core.*importorg.trustweave.did.*// Kotlinx importsimportkotlinx.coroutines.runBlockingimportkotlinx.serialization.json.buildJsonObjectimportkotlinx.serialization.json.put/**
* Example: Creating and using a DID
*
* This example demonstrates:
* - Creating a DID with default options
* - Resolving a DID
* - Error handling patterns
*/funmain()=runBlocking{// Create TrustWeave instancevalTrustWeave=TrustWeave.create()// Create DID with error handlingvaldidResult=TrustWeave.createDid()didResult.fold(onSuccess={did->println("Created DID: ${did.id}")// Resolve the DIDvalresolution=TrustWeave.resolveDid(did.id).getOrThrow()if(resolution.document!=null){println("Resolved DID document")}},onFailure={error->when(error){isTrustWeaveError.DidMethodNotRegistered->{println("Method not registered: ${error.method}")}else->{println("Error: ${error.message}")}}})}
Best Practices
Always include imports for complete examples
Use descriptive variable names that match domain terminology
Add context comments when using patterns that might not be obvious
Show error handling appropriate to the context
Include expected output for complete examples
Tag version-specific examples
Use consistent formatting (ktlint-compliant)
Anti-Patterns to Avoid
❌ Missing imports - Examples won’t compile
❌ Inconsistent error handling - Confuses readers
❌ Unclear variable names - Hard to understand
❌ Missing context - Readers don’t know when to use pattern
❌ Incomplete examples - Can’t run as-is
❌ Version-specific code without tags - May not work in all versions