You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: generator/README.md
+23-26Lines changed: 23 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,27 @@
1
-
=Dropbox Go SDK Generator
1
+
#Dropbox Go SDK Generator
2
2
3
-
This directory contains the https://github.com/dropbox/stone[Stone] code generators
4
-
used to programmatically generate the https://github.com/dropbox/dropbox-sdk-go[Dropbox Go SDK].
3
+
This directory contains the [Stone](https://github.com/dropbox/stone) code generators
4
+
used to programmatically generate the [Dropbox Go SDK](https://github.com/dropbox/dropbox-sdk-go).
5
5
6
-
==Requirements
6
+
##Requirements
7
7
8
8
* While not a hard requirement, this repo currently assumes `python3` in the path.
9
-
* Assumes you have already installed https://github.com/dropbox/stone[Stone]
10
-
* Requires https://godoc.org/golang.org/x/tools/cmd/goimports[goimports] to fix up imports in the auto-generated code
9
+
* Assumes you have already installed [Stone](https://github.com/dropbox/stone)
10
+
* Requires [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports) to fix up imports in the auto-generated code
11
11
12
-
== Basic Setup
12
+
##Basic Setup
13
13
14
14
. Clone this repo
15
15
. Run `git submodule init` followed by `git submodule update`
16
16
. Run `./generate-sdk.sh` to generate code under `../dropbox`
17
17
18
-
== Generated Code
18
+
##Generated Code
19
19
20
-
=== Structs
20
+
###Structs
21
21
22
22
Stone https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#struct[structs] are represented as Go https://gobyexample.com/structs[structs] in a relatively straight-forward manner.
23
23
24
-
----
24
+
```
25
25
struct Account <1>
26
26
"The amount of detail revealed about an account depends on the user
27
27
being queried and the user making the query." <2>
@@ -30,10 +30,9 @@ struct Account <1>
30
30
"The user's unique Dropbox ID." <4>
31
31
name Name <5>
32
32
"Details of a user's name."
33
-
----
33
+
```
34
34
35
-
[source,go]
36
-
----
35
+
```go
37
36
// The amount of detail revealed about an account depends on the user being
38
37
// queried and the user making the query. <2>
39
38
typeAccountstruct { // <1>
@@ -42,29 +41,28 @@ type Account struct { // <1>
42
41
// Details of a user's name.
43
42
Name *Name `json:"name"`// <5>
44
43
}
45
-
----
44
+
```
46
45
<1> A struct is defined as a Go struct
47
46
<2> The documentation shows up before the struct definition
48
47
<3> Each struct member is exported and also gets assigned the correct json tag. The latter is used for serializing requests and deserializing responses.
49
48
<4> Member documentation appears above the member definition
50
49
<5> Non-primitive types are represented as pointers to the corresponding type
51
50
52
-
=== Unions
51
+
###Unions
53
52
54
53
Stone https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#union[unions] are bit more complex as Go doesn't have native support for union types (tagged or otherwise). We declare a union as a Go struct with all the possible fields as pointer types, and then use the tag value to populate the correct field during deserialization. This necessitates the use of an intermedia wrapper struct for the deserialization to work correctly, see below for a concrete example.
55
54
56
-
----
55
+
```
57
56
union SpaceAllocation
58
57
"Space is allocated differently based on the type of account."
59
58
60
59
individual IndividualSpaceAllocation
61
60
"The user's space allocation applies only to their individual account."
62
61
team TeamSpaceAllocation
63
62
"The user shares space with other members of their team."
64
-
----
63
+
```
65
64
66
-
[source,go]
67
-
----
65
+
```go
68
66
// Space is allocated differently based on the type of account.
<2> The tag value is used to determine which field is actually set
109
107
<3> Possible values are represented as pointer types. Note the `omitempty` in the JSON tag; this is so values not set are automatically elided during serialization.
<7> When we deserialize response into the wrapper struct, it should get the tag value and the raw JSON as part of the members.
114
112
<8> We then use the tag value to deserialize the `RawMessage` into the appropriate member of the union type
115
113
116
-
=== Struct with Enumerated Subtypes
114
+
###Struct with Enumerated Subtypes
117
115
118
116
Per the https://github.com/dropbox/stone/blob/master/doc/lang_ref.rst#struct-polymorphism[spec], structs with enumerated subtypes are a mechanism of inheritance:
119
117
120
118
> If a struct enumerates its subtypes, an instance of any subtype will satisfy the type constraint.
121
119
122
120
So to represent structs with enumerated subtypes in Go, we use a strategy similar to unions. The _base_ struct (the one that defines the subtypes) is represented like we represent a union above. The _subtype_ is represented as a struct that essentially duplicates all fields of the base type and includes fields specific to that subtype. Here's an example:
123
121
124
-
----
122
+
```
125
123
struct Metadata
126
124
"Metadata for a file or folder."
127
125
@@ -138,13 +136,12 @@ struct Metadata
138
136
struct FileMetadata extends Metadata
139
137
id Id? #<2>
140
138
...
141
-
----
139
+
```
142
140
<1> Field common to all subtypes
143
141
<2> Field specific to `FileMetadata`
144
142
145
143
146
-
[source,go]
147
-
----
144
+
```go
148
145
// Metadata for a file or folder.
149
146
typeMetadatastruct { // <1>
150
147
Tagstring`json:".tag"`
@@ -161,7 +158,7 @@ type FileMetadata struct {
161
158
...
162
159
Idstring`json:"id,omitempty"`// <3>
163
160
}
164
-
----
161
+
```
165
162
<1> Subtype is represented like we represent unions as described above
166
163
<2> Common fields are duplicated in subtypes
167
164
<3> Subtype specific fields are included as usual in structs
0 commit comments