Skip to content

Commit e18ca8a

Browse files
authored
Reformat README in Markdown
1 parent db6c275 commit e18ca8a

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed
Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
= Dropbox Go SDK Generator
1+
# Dropbox Go SDK Generator
22

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).
55

6-
== Requirements
6+
## Requirements
77

88
* 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
1111

12-
== Basic Setup
12+
## Basic Setup
1313

1414
. Clone this repo
1515
. Run `git submodule init` followed by `git submodule update`
1616
. Run `./generate-sdk.sh` to generate code under `../dropbox`
1717

18-
== Generated Code
18+
## Generated Code
1919

20-
=== Structs
20+
### Structs
2121

2222
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.
2323

24-
----
24+
```
2525
struct Account <1>
2626
"The amount of detail revealed about an account depends on the user
2727
being queried and the user making the query." <2>
@@ -30,10 +30,9 @@ struct Account <1>
3030
"The user's unique Dropbox ID." <4>
3131
name Name <5>
3232
"Details of a user's name."
33-
----
33+
```
3434

35-
[source,go]
36-
----
35+
```go
3736
// The amount of detail revealed about an account depends on the user being
3837
// queried and the user making the query. <2>
3938
type Account struct { // <1>
@@ -42,29 +41,28 @@ type Account struct { // <1>
4241
// Details of a user's name.
4342
Name *Name `json:"name"` // <5>
4443
}
45-
----
44+
```
4645
<1> A struct is defined as a Go struct
4746
<2> The documentation shows up before the struct definition
4847
<3> Each struct member is exported and also gets assigned the correct json tag. The latter is used for serializing requests and deserializing responses.
4948
<4> Member documentation appears above the member definition
5049
<5> Non-primitive types are represented as pointers to the corresponding type
5150

52-
=== Unions
51+
### Unions
5352

5453
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.
5554

56-
----
55+
```
5756
union SpaceAllocation
5857
"Space is allocated differently based on the type of account."
5958
6059
individual IndividualSpaceAllocation
6160
"The user's space allocation applies only to their individual account."
6261
team TeamSpaceAllocation
6362
"The user shares space with other members of their team."
64-
----
63+
```
6564

66-
[source,go]
67-
----
65+
```go
6866
// Space is allocated differently based on the type of account.
6967
type SpaceAllocation struct { // <1>
7068
Tag string `json:".tag"` // <2>
@@ -103,7 +101,7 @@ func (u *SpaceAllocation) UnmarshalJSON(body []byte) error { // <4>
103101
}
104102
return nil
105103
}
106-
----
104+
```
107105
<1> A babel union is represented as Go struct
108106
<2> The tag value is used to determine which field is actually set
109107
<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.
@@ -113,15 +111,15 @@ func (u *SpaceAllocation) UnmarshalJSON(body []byte) error { // <4>
113111
<7> When we deserialize response into the wrapper struct, it should get the tag value and the raw JSON as part of the members.
114112
<8> We then use the tag value to deserialize the `RawMessage` into the appropriate member of the union type
115113

116-
=== Struct with Enumerated Subtypes
114+
### Struct with Enumerated Subtypes
117115

118116
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:
119117

120118
> If a struct enumerates its subtypes, an instance of any subtype will satisfy the type constraint.
121119
122120
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:
123121

124-
----
122+
```
125123
struct Metadata
126124
"Metadata for a file or folder."
127125
@@ -138,13 +136,12 @@ struct Metadata
138136
struct FileMetadata extends Metadata
139137
id Id? #<2>
140138
...
141-
----
139+
```
142140
<1> Field common to all subtypes
143141
<2> Field specific to `FileMetadata`
144142

145143

146-
[source,go]
147-
----
144+
```go
148145
// Metadata for a file or folder.
149146
type Metadata struct { // <1>
150147
Tag string `json:".tag"`
@@ -161,7 +158,7 @@ type FileMetadata struct {
161158
...
162159
Id string `json:"id,omitempty"` // <3>
163160
}
164-
----
161+
```
165162
<1> Subtype is represented like we represent unions as described above
166163
<2> Common fields are duplicated in subtypes
167164
<3> Subtype specific fields are included as usual in structs

0 commit comments

Comments
 (0)