@alloy-js/go 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/fmt/fmt.d.ts +147 -0
  5. package/dist/src/builtins/fmt/fmt.d.ts.map +1 -0
  6. package/dist/src/builtins/fmt/fmt.js +148 -0
  7. package/dist/src/builtins/fmt/fmt.js.map +1 -0
  8. package/dist/src/builtins/index.d.ts +2004 -0
  9. package/dist/src/builtins/index.d.ts.map +1 -0
  10. package/dist/src/builtins/index.js +9 -0
  11. package/dist/src/builtins/index.js.map +1 -0
  12. package/dist/src/builtins/io/io.d.ts +648 -0
  13. package/dist/src/builtins/io/io.d.ts.map +1 -0
  14. package/dist/src/builtins/io/io.js +650 -0
  15. package/dist/src/builtins/io/io.js.map +1 -0
  16. package/dist/src/builtins/net/net.d.ts +1210 -0
  17. package/dist/src/builtins/net/net.d.ts.map +1 -0
  18. package/dist/src/builtins/net/net.js +1212 -0
  19. package/dist/src/builtins/net/net.js.map +1 -0
  20. package/dist/src/components/ImportStatement.d.ts +15 -0
  21. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  22. package/dist/src/components/ImportStatement.js +90 -0
  23. package/dist/src/components/ImportStatement.js.map +1 -0
  24. package/dist/src/components/ModuleDirectory.d.ts +8 -0
  25. package/dist/src/components/ModuleDirectory.d.ts.map +1 -0
  26. package/dist/src/components/ModuleDirectory.js +20 -0
  27. package/dist/src/components/ModuleDirectory.js.map +1 -0
  28. package/dist/src/components/Name.d.ts +2 -0
  29. package/dist/src/components/Name.d.ts.map +1 -0
  30. package/dist/src/components/Name.js +12 -0
  31. package/dist/src/components/Name.js.map +1 -0
  32. package/dist/src/components/Reference.d.ts +6 -0
  33. package/dist/src/components/Reference.d.ts.map +1 -0
  34. package/dist/src/components/Reference.js +15 -0
  35. package/dist/src/components/Reference.js.map +1 -0
  36. package/dist/src/components/SourceDirectory.d.ts +6 -0
  37. package/dist/src/components/SourceDirectory.d.ts.map +1 -0
  38. package/dist/src/components/SourceDirectory.js +30 -0
  39. package/dist/src/components/SourceDirectory.js.map +1 -0
  40. package/dist/src/components/SourceFile.d.ts +16 -0
  41. package/dist/src/components/SourceFile.d.ts.map +1 -0
  42. package/dist/src/components/SourceFile.js +75 -0
  43. package/dist/src/components/SourceFile.js.map +1 -0
  44. package/dist/src/components/doc/comment.d.ts +21 -0
  45. package/dist/src/components/doc/comment.d.ts.map +1 -0
  46. package/dist/src/components/doc/comment.js +30 -0
  47. package/dist/src/components/doc/comment.js.map +1 -0
  48. package/dist/src/components/doc/comment.test.d.ts +2 -0
  49. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  50. package/dist/src/components/doc/comment.test.js +215 -0
  51. package/dist/src/components/doc/comment.test.js.map +1 -0
  52. package/dist/src/components/function/function.d.ts +31 -0
  53. package/dist/src/components/function/function.d.ts.map +1 -0
  54. package/dist/src/components/function/function.js +134 -0
  55. package/dist/src/components/function/function.js.map +1 -0
  56. package/dist/src/components/function/function.test.d.ts +2 -0
  57. package/dist/src/components/function/function.test.d.ts.map +1 -0
  58. package/dist/src/components/function/function.test.js +410 -0
  59. package/dist/src/components/function/function.test.js.map +1 -0
  60. package/dist/src/components/index.d.ts +16 -0
  61. package/dist/src/components/index.d.ts.map +1 -0
  62. package/dist/src/components/index.js +16 -0
  63. package/dist/src/components/index.js.map +1 -0
  64. package/dist/src/components/interface/declaration.d.ts +68 -0
  65. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  66. package/dist/src/components/interface/declaration.js +181 -0
  67. package/dist/src/components/interface/declaration.js.map +1 -0
  68. package/dist/src/components/interface/interface.test.d.ts +2 -0
  69. package/dist/src/components/interface/interface.test.d.ts.map +1 -0
  70. package/dist/src/components/interface/interface.test.js +517 -0
  71. package/dist/src/components/interface/interface.test.js.map +1 -0
  72. package/dist/src/components/package.test.d.ts +2 -0
  73. package/dist/src/components/package.test.d.ts.map +1 -0
  74. package/dist/src/components/package.test.js +89 -0
  75. package/dist/src/components/package.test.js.map +1 -0
  76. package/dist/src/components/parameters/parameters.d.ts +15 -0
  77. package/dist/src/components/parameters/parameters.d.ts.map +1 -0
  78. package/dist/src/components/parameters/parameters.js +58 -0
  79. package/dist/src/components/parameters/parameters.js.map +1 -0
  80. package/dist/src/components/parameters/typeparameters.d.ts +25 -0
  81. package/dist/src/components/parameters/typeparameters.d.ts.map +1 -0
  82. package/dist/src/components/parameters/typeparameters.js +53 -0
  83. package/dist/src/components/parameters/typeparameters.js.map +1 -0
  84. package/dist/src/components/pointer/pointer.d.ts +9 -0
  85. package/dist/src/components/pointer/pointer.d.ts.map +1 -0
  86. package/dist/src/components/pointer/pointer.js +8 -0
  87. package/dist/src/components/pointer/pointer.js.map +1 -0
  88. package/dist/src/components/stc/index.d.ts +27 -0
  89. package/dist/src/components/stc/index.d.ts.map +1 -0
  90. package/dist/src/components/stc/index.js +27 -0
  91. package/dist/src/components/stc/index.js.map +1 -0
  92. package/dist/src/components/struct/declaration.d.ts +62 -0
  93. package/dist/src/components/struct/declaration.d.ts.map +1 -0
  94. package/dist/src/components/struct/declaration.js +161 -0
  95. package/dist/src/components/struct/declaration.js.map +1 -0
  96. package/dist/src/components/struct/struct.test.d.ts +2 -0
  97. package/dist/src/components/struct/struct.test.d.ts.map +1 -0
  98. package/dist/src/components/struct/struct.test.js +741 -0
  99. package/dist/src/components/struct/struct.test.js.map +1 -0
  100. package/dist/src/components/type/declaration.d.ts +29 -0
  101. package/dist/src/components/type/declaration.d.ts.map +1 -0
  102. package/dist/src/components/type/declaration.js +63 -0
  103. package/dist/src/components/type/declaration.js.map +1 -0
  104. package/dist/src/components/var/declaration.d.ts +27 -0
  105. package/dist/src/components/var/declaration.d.ts.map +1 -0
  106. package/dist/src/components/var/declaration.js +84 -0
  107. package/dist/src/components/var/declaration.js.map +1 -0
  108. package/dist/src/components/var/var-declaration.test.d.ts +2 -0
  109. package/dist/src/components/var/var-declaration.test.d.ts.map +1 -0
  110. package/dist/src/components/var/var-declaration.test.js +340 -0
  111. package/dist/src/components/var/var-declaration.test.js.map +1 -0
  112. package/dist/src/context/package.d.ts +9 -0
  113. package/dist/src/context/package.d.ts.map +1 -0
  114. package/dist/src/context/package.js +6 -0
  115. package/dist/src/context/package.js.map +1 -0
  116. package/dist/src/create-module.d.ts +47 -0
  117. package/dist/src/create-module.d.ts.map +1 -0
  118. package/dist/src/create-module.js +158 -0
  119. package/dist/src/create-module.js.map +1 -0
  120. package/dist/src/create-module.test.d.ts +2 -0
  121. package/dist/src/create-module.test.d.ts.map +1 -0
  122. package/dist/src/create-module.test.js +147 -0
  123. package/dist/src/create-module.test.js.map +1 -0
  124. package/dist/src/index.d.ts +6 -0
  125. package/dist/src/index.d.ts.map +1 -0
  126. package/dist/src/index.js +6 -0
  127. package/dist/src/index.js.map +1 -0
  128. package/dist/src/name-policy.d.ts +5 -0
  129. package/dist/src/name-policy.d.ts.map +1 -0
  130. package/dist/src/name-policy.js +27 -0
  131. package/dist/src/name-policy.js.map +1 -0
  132. package/dist/src/scopes/contexts.d.ts +9 -0
  133. package/dist/src/scopes/contexts.d.ts.map +1 -0
  134. package/dist/src/scopes/contexts.js +34 -0
  135. package/dist/src/scopes/contexts.js.map +1 -0
  136. package/dist/src/scopes/factories.d.ts +7 -0
  137. package/dist/src/scopes/factories.d.ts.map +1 -0
  138. package/dist/src/scopes/factories.js +16 -0
  139. package/dist/src/scopes/factories.js.map +1 -0
  140. package/dist/src/scopes/function.d.ts +9 -0
  141. package/dist/src/scopes/function.d.ts.map +1 -0
  142. package/dist/src/scopes/function.js +14 -0
  143. package/dist/src/scopes/function.js.map +1 -0
  144. package/dist/src/scopes/go.d.ts +8 -0
  145. package/dist/src/scopes/go.d.ts.map +1 -0
  146. package/dist/src/scopes/go.js +12 -0
  147. package/dist/src/scopes/go.js.map +1 -0
  148. package/dist/src/scopes/index.d.ts +10 -0
  149. package/dist/src/scopes/index.d.ts.map +1 -0
  150. package/dist/src/scopes/index.js +10 -0
  151. package/dist/src/scopes/index.js.map +1 -0
  152. package/dist/src/scopes/lexical.d.ts +8 -0
  153. package/dist/src/scopes/lexical.d.ts.map +1 -0
  154. package/dist/src/scopes/lexical.js +11 -0
  155. package/dist/src/scopes/lexical.js.map +1 -0
  156. package/dist/src/scopes/module.d.ts +9 -0
  157. package/dist/src/scopes/module.d.ts.map +1 -0
  158. package/dist/src/scopes/module.js +31 -0
  159. package/dist/src/scopes/module.js.map +1 -0
  160. package/dist/src/scopes/named-type.d.ts +23 -0
  161. package/dist/src/scopes/named-type.d.ts.map +1 -0
  162. package/dist/src/scopes/named-type.js +33 -0
  163. package/dist/src/scopes/named-type.js.map +1 -0
  164. package/dist/src/scopes/package.d.ts +11 -0
  165. package/dist/src/scopes/package.d.ts.map +1 -0
  166. package/dist/src/scopes/package.js +39 -0
  167. package/dist/src/scopes/package.js.map +1 -0
  168. package/dist/src/scopes/source-file.d.ts +17 -0
  169. package/dist/src/scopes/source-file.d.ts.map +1 -0
  170. package/dist/src/scopes/source-file.js +42 -0
  171. package/dist/src/scopes/source-file.js.map +1 -0
  172. package/dist/src/symbols/factories.d.ts +25 -0
  173. package/dist/src/symbols/factories.d.ts.map +1 -0
  174. package/dist/src/symbols/factories.js +114 -0
  175. package/dist/src/symbols/factories.js.map +1 -0
  176. package/dist/src/symbols/function.d.ts +10 -0
  177. package/dist/src/symbols/function.d.ts.map +1 -0
  178. package/dist/src/symbols/function.js +12 -0
  179. package/dist/src/symbols/function.js.map +1 -0
  180. package/dist/src/symbols/go.d.ts +22 -0
  181. package/dist/src/symbols/go.d.ts.map +1 -0
  182. package/dist/src/symbols/go.js +56 -0
  183. package/dist/src/symbols/go.js.map +1 -0
  184. package/dist/src/symbols/index.d.ts +7 -0
  185. package/dist/src/symbols/index.d.ts.map +1 -0
  186. package/dist/src/symbols/index.js +7 -0
  187. package/dist/src/symbols/index.js.map +1 -0
  188. package/dist/src/symbols/named-type.d.ts +24 -0
  189. package/dist/src/symbols/named-type.d.ts.map +1 -0
  190. package/dist/src/symbols/named-type.js +53 -0
  191. package/dist/src/symbols/named-type.js.map +1 -0
  192. package/dist/src/symbols/package.d.ts +18 -0
  193. package/dist/src/symbols/package.d.ts.map +1 -0
  194. package/dist/src/symbols/package.js +38 -0
  195. package/dist/src/symbols/package.js.map +1 -0
  196. package/dist/src/symbols/reference.d.ts +3 -0
  197. package/dist/src/symbols/reference.d.ts.map +1 -0
  198. package/dist/src/symbols/reference.js +76 -0
  199. package/dist/src/symbols/reference.js.map +1 -0
  200. package/dist/test/utils.d.ts +8 -0
  201. package/dist/test/utils.d.ts.map +1 -0
  202. package/dist/test/utils.js +68 -0
  203. package/dist/test/utils.js.map +1 -0
  204. package/dist/test/vitest.setup.d.ts +2 -0
  205. package/dist/test/vitest.setup.d.ts.map +1 -0
  206. package/dist/test/vitest.setup.js +2 -0
  207. package/dist/test/vitest.setup.js.map +1 -0
  208. package/dist/tsconfig.tsbuildinfo +1 -0
  209. package/package.json +45 -0
  210. package/src/builtins/fmt/fmt.ts +78 -0
  211. package/src/builtins/index.ts +9 -0
  212. package/src/builtins/io/io.ts +355 -0
  213. package/src/builtins/net/net.ts +502 -0
  214. package/src/components/ImportStatement.tsx +115 -0
  215. package/src/components/ModuleDirectory.tsx +18 -0
  216. package/src/components/Name.tsx +11 -0
  217. package/src/components/Reference.tsx +18 -0
  218. package/src/components/SourceDirectory.tsx +32 -0
  219. package/src/components/SourceFile.tsx +74 -0
  220. package/src/components/doc/comment.test.tsx +278 -0
  221. package/src/components/doc/comment.tsx +40 -0
  222. package/src/components/function/function.test.tsx +387 -0
  223. package/src/components/function/function.tsx +194 -0
  224. package/src/components/index.ts +15 -0
  225. package/src/components/interface/declaration.tsx +250 -0
  226. package/src/components/interface/interface.test.tsx +390 -0
  227. package/src/components/package.test.tsx +63 -0
  228. package/src/components/parameters/parameters.tsx +74 -0
  229. package/src/components/parameters/typeparameters.tsx +78 -0
  230. package/src/components/pointer/pointer.tsx +12 -0
  231. package/src/components/stc/index.ts +27 -0
  232. package/src/components/struct/declaration.tsx +228 -0
  233. package/src/components/struct/struct.test.tsx +558 -0
  234. package/src/components/type/declaration.tsx +93 -0
  235. package/src/components/var/declaration.tsx +138 -0
  236. package/src/components/var/var-declaration.test.tsx +236 -0
  237. package/src/context/package.ts +13 -0
  238. package/src/create-module.test.tsx +128 -0
  239. package/src/create-module.ts +347 -0
  240. package/src/index.ts +5 -0
  241. package/src/name-policy.ts +65 -0
  242. package/src/scopes/contexts.ts +45 -0
  243. package/src/scopes/factories.ts +29 -0
  244. package/src/scopes/function.ts +22 -0
  245. package/src/scopes/go.ts +18 -0
  246. package/src/scopes/index.ts +9 -0
  247. package/src/scopes/lexical.ts +14 -0
  248. package/src/scopes/module.ts +35 -0
  249. package/src/scopes/named-type.ts +49 -0
  250. package/src/scopes/package.ts +55 -0
  251. package/src/scopes/source-file.ts +66 -0
  252. package/src/symbols/factories.ts +234 -0
  253. package/src/symbols/function.ts +17 -0
  254. package/src/symbols/go.ts +82 -0
  255. package/src/symbols/index.ts +6 -0
  256. package/src/symbols/named-type.ts +90 -0
  257. package/src/symbols/package.ts +54 -0
  258. package/src/symbols/reference.ts +87 -0
  259. package/temp/api.json +8753 -0
  260. package/test/utils.tsx +77 -0
  261. package/test/vitest.setup.ts +1 -0
  262. package/tsconfig.json +15 -0
  263. package/tsdoc-metadata.json +11 -0
  264. package/vitest.config.ts +13 -0
@@ -0,0 +1,74 @@
1
+ import {
2
+ Children,
3
+ createSymbolSlot,
4
+ Declaration,
5
+ For,
6
+ Indent,
7
+ Namekey,
8
+ Refkey,
9
+ } from "@alloy-js/core";
10
+ import { createParameterSymbol } from "../../symbols/factories.js";
11
+ import { Name } from "../Name.jsx";
12
+
13
+ export interface FunctionParameterProps {
14
+ name: string | Namekey;
15
+ type: Children;
16
+ variadic?: boolean;
17
+ refkey?: Refkey;
18
+ }
19
+
20
+ /** Define a parameter to be used in functions. */
21
+ export function FunctionParameter(props: FunctionParameterProps) {
22
+ const TypeSlot = createSymbolSlot();
23
+
24
+ const memberSymbol = createParameterSymbol(props.name, {
25
+ refkeys: props.refkey,
26
+ type: TypeSlot.firstSymbol,
27
+ });
28
+
29
+ return (
30
+ <Declaration symbol={memberSymbol}>
31
+ <Name /> {props.variadic ? "..." : null}
32
+ <TypeSlot>{props.type}</TypeSlot>
33
+ </Declaration>
34
+ );
35
+ }
36
+
37
+ export interface FunctionParametersProps {
38
+ parameters: FunctionParameterProps[] | undefined;
39
+ }
40
+
41
+ /** Render a collection of parameters */
42
+ export function FunctionParameters(props: FunctionParametersProps) {
43
+ const params = props.parameters ?? [];
44
+ for (let i = 0; i < (params.length ?? 0); i++) {
45
+ if (i < params.length - 1 && params[i].variadic) {
46
+ throw new Error("Variadic parameter must be the last parameter.");
47
+ }
48
+ }
49
+ return (
50
+ <group>
51
+ {"("}
52
+ {props.parameters && (
53
+ <Indent softline>
54
+ <For
55
+ each={params}
56
+ joiner={
57
+ <>
58
+ {","}
59
+ <ifBreak flatContents=" ">
60
+ <sbr />
61
+ </ifBreak>
62
+ </>
63
+ }
64
+ ender={<ifBreak>,</ifBreak>}
65
+ >
66
+ {(param) => <FunctionParameter {...param} />}
67
+ </For>
68
+ </Indent>
69
+ )}
70
+ <sbr />
71
+ {")"}
72
+ </group>
73
+ );
74
+ }
@@ -0,0 +1,78 @@
1
+ import {
2
+ Children,
3
+ For,
4
+ Indent,
5
+ MemberDeclaration,
6
+ MemberName,
7
+ Namekey,
8
+ Refkey,
9
+ } from "@alloy-js/core";
10
+ import { createTypeParameterSymbol } from "../../symbols/factories.js";
11
+
12
+ /**
13
+ * Information for a Go generic type parameter.
14
+ */
15
+ export interface TypeParameterProps {
16
+ /**
17
+ * The name of the type parameter.
18
+ */
19
+ name: string | Namekey;
20
+
21
+ /**
22
+ * The parameter constraint
23
+ */
24
+ constraint: Children;
25
+
26
+ /**
27
+ * A refkey for this type parameter.
28
+ */
29
+ refkey?: Refkey;
30
+ }
31
+
32
+ export function TypeParameter(props: TypeParameterProps) {
33
+ const symbol = createTypeParameterSymbol(props.name, {
34
+ refkeys: props.refkey,
35
+ });
36
+ return (
37
+ <MemberDeclaration symbol={symbol}>
38
+ <MemberName /> {props.constraint}
39
+ </MemberDeclaration>
40
+ );
41
+ }
42
+
43
+ export interface TypeParametersProps {
44
+ /** Parameters */
45
+ parameters?: TypeParameterProps[];
46
+ }
47
+
48
+ export function TypeParameters(props: TypeParametersProps) {
49
+ if (!props.parameters || props.parameters.length === 0) {
50
+ return null;
51
+ }
52
+
53
+ return (
54
+ <group>
55
+ {"["}
56
+ {props.parameters && (
57
+ <Indent softline>
58
+ <For
59
+ each={props.parameters}
60
+ joiner={
61
+ <>
62
+ {","}
63
+ <ifBreak flatContents=" ">
64
+ <sbr />
65
+ </ifBreak>
66
+ </>
67
+ }
68
+ ender={<ifBreak>,</ifBreak>}
69
+ >
70
+ {(param) => <TypeParameter {...param} />}
71
+ </For>
72
+ </Indent>
73
+ )}
74
+ <sbr />
75
+ {"]"}
76
+ </group>
77
+ );
78
+ }
@@ -0,0 +1,12 @@
1
+ import { Children } from "@alloy-js/core";
2
+
3
+ export interface PointerProps {
4
+ children: Children;
5
+ }
6
+
7
+ /**
8
+ * A Go pointer type that wraps the given type.
9
+ */
10
+ export function Pointer(props: PointerProps) {
11
+ return <>*{props.children}</>;
12
+ }
@@ -0,0 +1,27 @@
1
+ import * as core from "@alloy-js/core";
2
+ import * as base from "../index.js";
3
+
4
+ export const LineComment = core.stc(base.LineComment);
5
+ export const BlockComment = core.stc(base.BlockComment);
6
+ export const FunctionDeclaration = core.stc(base.FunctionDeclaration);
7
+ export const FunctionReceiver = core.stc(base.FunctionReceiver);
8
+ export const InterfaceDeclaration = core.stc(base.InterfaceDeclaration);
9
+ export const InterfaceFunction = core.stc(base.InterfaceFunction);
10
+ export const InterfaceEmbed = core.stc(base.InterfaceEmbed);
11
+ export const FunctionParameter = core.stc(base.FunctionParameter);
12
+ export const FunctionParameters = core.stc(base.FunctionParameters);
13
+ export const Pointer = core.stc(base.Pointer);
14
+ export const StructDeclaration = core.stc(base.StructDeclaration);
15
+ export const StructMember = core.stc(base.StructMember);
16
+ export const StructEmbed = core.stc(base.StructEmbed);
17
+ export const TypeDeclarationGroup = core.stc(base.TypeDeclarationGroup);
18
+ export const TypeDeclaration = core.stc(base.TypeDeclaration);
19
+ export const VariableDeclarationGroup = core.stc(base.VariableDeclarationGroup);
20
+ export const VariableDeclaration = core.stc(base.VariableDeclaration);
21
+ export const ImportStatements = core.stc(base.ImportStatements);
22
+ export const SingleImportStatement = core.stc(base.SingleImportStatement);
23
+ export const ModuleDirectory = core.stc(base.ModuleDirectory);
24
+ export const Name = core.stc(base.Name);
25
+ export const Reference = core.stc(base.Reference);
26
+ export const SourceDirectory = core.stc(base.SourceDirectory);
27
+ export const SourceFile = core.stc(base.SourceFile);
@@ -0,0 +1,228 @@
1
+ import {
2
+ Block,
3
+ Children,
4
+ computed,
5
+ Declaration,
6
+ DeclarationContext,
7
+ effect,
8
+ memo,
9
+ Namekey,
10
+ Refkey,
11
+ Scope,
12
+ Show,
13
+ takeSymbols,
14
+ untrack,
15
+ useContext,
16
+ watch,
17
+ } from "@alloy-js/core";
18
+ import { useGoScope, useNamedTypeScope } from "../../scopes/contexts.js";
19
+ import { createNamedTypeScope } from "../../scopes/factories.js";
20
+ import { GoNamedTypeScope } from "../../scopes/named-type.js";
21
+ import {
22
+ createAnonymousTypeSymbol,
23
+ createStructMemberSymbol,
24
+ } from "../../symbols/factories.js";
25
+ import { GoSymbol } from "../../symbols/go.js";
26
+ import { NamedTypeSymbol } from "../../symbols/named-type.js";
27
+ import { LineComment } from "../doc/comment.js";
28
+ import { Name } from "../Name.js";
29
+ import { TypeDeclaration, TypeDeclarationProps } from "../type/declaration.jsx";
30
+
31
+ /**
32
+ * Properties for creating a struct declaration.
33
+ * This can be used for both named and anonymous structs.
34
+ * For named structs, use `StructTypeDeclarationProps` instead.
35
+ */
36
+ export interface StructDeclarationProps {
37
+ refkey?: Refkey;
38
+ children?: Children;
39
+
40
+ /**
41
+ * Whether to render the struct in a single line.
42
+ * This will only compile if `children` is a single line as well.
43
+ * This is not common in Go, so use with caution.
44
+ */
45
+ singleLine?: boolean;
46
+ }
47
+
48
+ /**
49
+ * Properties for creating a named struct type declaration.
50
+ * This is a combination of `StructDeclarationProps` and `TypeDeclarationProps`.
51
+ */
52
+ export interface StructTypeDeclarationProps
53
+ extends StructDeclarationProps,
54
+ TypeDeclarationProps {}
55
+
56
+ /**
57
+ * Wrapper for creating a named struct type declaration.
58
+ */
59
+ export function StructTypeDeclaration(props: StructTypeDeclarationProps) {
60
+ return (
61
+ <TypeDeclaration {...props}>
62
+ <StructDeclaration {...props} />
63
+ </TypeDeclaration>
64
+ );
65
+ }
66
+
67
+ /**
68
+ * Go struct declaration.
69
+ * @example
70
+ * ```tsx
71
+ * <StructDeclaration>
72
+ * <StructEmbed pointer>{MyOtherStruct}</StructEmbed>
73
+ * <StructMember exported name="MyProperty" type="int" />
74
+ * </StructDeclaration>
75
+ * ```
76
+ * This will produce:
77
+ * ```go
78
+ * struct {
79
+ * MyOtherStruct
80
+ * MyProperty int
81
+ * }
82
+ * ```
83
+ */
84
+ export function StructDeclaration(props: StructDeclarationProps) {
85
+ const scope = useGoScope();
86
+ let structScope: GoNamedTypeScope | undefined;
87
+ if (
88
+ !(scope instanceof GoNamedTypeScope) ||
89
+ scope.ownerSymbol.typeKind !== "type"
90
+ ) {
91
+ const declSymbol = useContext(DeclarationContext) as
92
+ | NamedTypeSymbol
93
+ | undefined;
94
+ // this is an anonymous struct
95
+ if (!declSymbol) {
96
+ // this can only happen when instantiating a struct directly
97
+ const anonymousSymbol = createAnonymousTypeSymbol("struct");
98
+ const anonymousScope = createNamedTypeScope(anonymousSymbol);
99
+ structScope = anonymousScope;
100
+ } else {
101
+ // this is most likely a struct field
102
+ const anonymousScope = createNamedTypeScope(declSymbol);
103
+ structScope = anonymousScope;
104
+ }
105
+ } else {
106
+ scope.ownerSymbol.typeKind = "struct";
107
+ }
108
+
109
+ const content = memo(() => {
110
+ if (props.children) {
111
+ if (props.singleLine) {
112
+ return (
113
+ <>
114
+ {"{"} {props.children} {"}"}
115
+ </>
116
+ );
117
+ } else {
118
+ return (
119
+ <>
120
+ {" "}
121
+ <Block>{props.children}</Block>
122
+ </>
123
+ );
124
+ }
125
+ } else {
126
+ return "{}";
127
+ }
128
+ });
129
+
130
+ return (
131
+ <>
132
+ struct
133
+ {structScope ?
134
+ <Scope value={structScope}>{content}</Scope>
135
+ : content}
136
+ </>
137
+ );
138
+ }
139
+
140
+ export interface StructMemberProps {
141
+ name: string | Namekey;
142
+ type: Children;
143
+ refkey?: Refkey;
144
+ tag?: string | Record<string, string>;
145
+ /** Doc comment */
146
+ doc?: Children;
147
+ }
148
+
149
+ export function StructMember(props: StructMemberProps) {
150
+ const symbol = createStructMemberSymbol(props.name, {
151
+ refkeys: props.refkey,
152
+ });
153
+
154
+ const tagString = computed(() => {
155
+ if (typeof props.tag === "string") {
156
+ return props.tag;
157
+ } else if (typeof props.tag === "object") {
158
+ return Object.entries(props.tag)
159
+ .sort((a, b) => a[0].localeCompare(b[0]))
160
+ .map(([key, value]) => `${key}:"${value}"`)
161
+ .join(" ");
162
+ }
163
+ return "";
164
+ });
165
+
166
+ return (
167
+ <Declaration symbol={symbol}>
168
+ <Show when={Boolean(props.doc)}>
169
+ <LineComment children={props.doc} />
170
+ <hbr />
171
+ </Show>
172
+ <Name /> {props.type}
173
+ {tagString.value === "" ?
174
+ ""
175
+ : tagString.value.includes('"') ?
176
+ ` \`${tagString.value}\``
177
+ : ` "${tagString.value}"`}
178
+ </Declaration>
179
+ );
180
+ }
181
+
182
+ export interface StructEmbedProps {
183
+ refkey?: Refkey;
184
+ children?: Children;
185
+ /** Doc comment */
186
+ doc?: Children;
187
+ }
188
+
189
+ export function StructEmbed(props: StructEmbedProps) {
190
+ let typeName: string = "<Unresolved symbol>";
191
+ if (props.children) {
192
+ if (typeof props.children === "string") {
193
+ typeName = props.children;
194
+ }
195
+ }
196
+
197
+ const memberSymbol = createStructMemberSymbol(typeName, {
198
+ refkeys: props.refkey,
199
+ });
200
+
201
+ const taken = takeSymbols();
202
+ effect(() => {
203
+ if (taken.size !== 1) return;
204
+ untrack(() => {
205
+ const symbol = Array.from(taken)[0] as GoSymbol;
206
+ memberSymbol.name = symbol.name;
207
+ watch(
208
+ () => symbol.name,
209
+ () => {
210
+ memberSymbol.name = symbol.name;
211
+ },
212
+ );
213
+ const namedTypeScope = useNamedTypeScope();
214
+ // TODO: somehow mark it as "promoted" such that it cannot be constructed directly
215
+ symbol.copyMembersTo(namedTypeScope.ownerSymbol);
216
+ });
217
+ });
218
+
219
+ return (
220
+ <Declaration symbol={memberSymbol}>
221
+ <Show when={Boolean(props.doc)}>
222
+ <LineComment children={props.doc} />
223
+ <hbr />
224
+ </Show>
225
+ {props.children}
226
+ </Declaration>
227
+ );
228
+ }