Debian Codegraph @

internal/website/content/concepts/structure/index.md

raw
1 ---
2 title: "Structuring Portable Code"
3 date: 2019-06-03T07:34:22-07:00
4 weight: 1
5 ---
6
7 The Go CDK's APIs are intentionally structured to make it easier to separate
8 your application's core logic from the details of the services it is using.
9
10 <!--more-->
11
12 ## Motivation
13
14 Consider the [uploader tutorial][]. Without the Go CDK, we would have had to
15 write a code path for Amazon's Simple Storage Service (S3) and another code
16 path for Google Cloud Storage (GCS). That would work, but it would be
17 tedious. We would have to learn the semantics of uploading files to both blob
18 storage services. Even worse, we would have two code paths that effectively
19 do the same thing, but would have to be maintained separately. It would be
20 much nicer if we could write the upload logic once and reuse it across
21 providers. That's exactly the kind of [separation of concerns][] that the Go
22 CDK makes possible.
23
24 (More details available in the [Go CDK design doc][Developers and Operators].)
25
26 [Developers and Operators]: https://github.com/google/go-cloud/blob/master/internal/docs/design.md#developers-and-operators
27 [separation of concerns]: https://en.wikipedia.org/wiki/Separation_of_concerns
28 [uploader tutorial]: {{< ref "/tutorials/cli-uploader.md" >}}
29
30 ## Portable Types and Drivers
31
32 The portable APIs that the Go CDK exports (like [`blob.Bucket`][] or
33 [`runtimevar.Variable`][]) are concrete types, not interfaces. To understand
34 why, imagine if we used a plain interface:
35
36 {{< figure class="FullWidthFigure" src="portable-type-no-driver.png" link="portable-type-no-driver.png" alt="Diagram showing user code depending on blob.Bucket, which is implemented by awsblob.Bucket." >}}
37
38 Consider the [`Bucket.NewWriter` method][], which infers the content type of the
39 blob based on the first bytes written to it. If `blob.Bucket` was an interface,
40 each implementation of `blob.Bucket` would have to replicate this behavior
41 precisely. This does not scale: conformance tests would be needed to ensure that
42 each interface method actually behaves in the way that the docs describe. This
43 makes the interfaces hard to implement, which runs counter to the goals of the
44 project.
45
46 Instead, we follow the example of [`database/sql`][] and separate out the
47 implementation-agnostic logic from the interface. The implementation-agnostic
48 logic-containing concrete type is the **portable type**. We call the interface
49 the **driver**. Visually, it looks like this:
50
51 {{< figure class="FullWidthFigure" src="portable-type.png" link="portable-type.png" alt="Diagram showing user code depending on blob.Bucket, which holds a driver.Bucket implemented by awsblob.Bucket." >}}
52
53 This has a number of benefits:
54
55 - The portable type can perform higher level logic without making the
56 interface complex to implement. In the blob example, the portable type's
57 `NewWriter` method can do the content type detection and then pass the final
58 result to the driver type.
59 - Methods can be added to the portable type without breaking compatibility.
60 Contrast with adding methods to an interface, which is a breaking change.
61 - When new operations on the driver are added as new optional interfaces, the
62 portable type can hide the need for type-assertions from the user.
63
64 (More details available in the [Go CDK design doc][Portable Types and Drivers].)
65
66 [Portable Types and Drivers]: https://github.com/google/go-cloud/blob/master/internal/docs/design.md#portable-types-and-drivers
67 [`blob.Bucket`]: https://godoc.org/github.com/google/go-cloud/blob#Bucket
68 [`runtimevar.Variable`]:
69 https://godoc.org/github.com/google/go-cloud/runtimevar#Variable
70 [`Bucket.NewWriter` method]:
71 https://godoc.org/github.com/google/go-cloud/blob#Bucket.NewWriter
72 [`database/sql`]: https://godoc.org/database/sql
73
74 ## Best Practices
75
76 - **Create portable types as close to program startup as possible.** Since
77 creation of a portable type requires using driver-specific setup, this
78 separates your driver-specific details from the rest of your application.
79 - **Pass portable types around as arguments or struct fields instead of as
80 package variables.** This allows you to easily swap out the portable type
81 for a local implementation in unit tests. It also enables you to use
82 dependency injection tools like [Wire][] to set up your application.
83 - **Avoid using [`As`][] functions when possible.** Using driver-specific
84 options makes it harder to test your code with confidence or migrate to
85 another driver later. If your application needs to use driver-specific
86 options, try to make it so that other drivers fall back gracefully. For
87 example, you may need to use a particular ACL setting for a write to a Google
88 Cloud Storage bucket. When testing for the driver-specific write options,
89 don't return an error if the `As` function doesn't have the right type. That
90 way, when running against an in-memory bucket for tests, the write will still
91 occur and can be observed. Leave provider-specific checks to integration
92 tests.
93
94 [`As`]: {{< ref "/concepts/as.md#as" >}}
95 [Wire]: https://github.com/google/wire