| 125 | } |
| 126 | |
| 127 | func ExampleOfMessageParserLambda() { |
| 128 | // 创建解析器 |
| 129 | parser := schema.NewMessageJSONParser[*MyStruct](&schema.MessageJSONParseConfig{ |
| 130 | ParseFrom: schema.MessageParseFromContent, |
| 131 | ParseKeyPath: "", // 如果仅需要 parse 子字段,可用 "key.sub.grandsub" |
| 132 | }) |
| 133 | |
| 134 | // 创建解析 Lambda |
| 135 | parserLambda := compose.MessageParser(parser) |
| 136 | |
| 137 | // 在 Chain 中使用 |
| 138 | chain := compose.NewChain[*schema.Message, *MyStruct]() |
| 139 | chain.AppendLambda(parserLambda) |
| 140 | |
| 141 | // 使用示例 |
| 142 | runner, _ := chain.Compile(context.Background()) |
| 143 | parsed, _ := runner.Invoke(context.Background(), &schema.Message{ |
| 144 | Content: `{"id": 1}`, |
| 145 | }) |
| 146 | // parsed.ID == 1 |
| 147 | |
| 148 | _ = parsed |
| 149 | } |