Debian Codegraph @

storage/2020-08-04/table/entities/insert.go

raw
1 package entities
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/Azure/go-autorest/autorest"
9 "github.com/Azure/go-autorest/autorest/azure"
10 "github.com/Azure/go-autorest/autorest/validation"
11 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
12 )
13
14 type InsertEntityInput struct {
15 // The level of MetaData provided for this Entity
16 MetaDataLevel MetaDataLevel
17
18 // The Entity which should be inserted, by default all values are strings
19 // To explicitly type a property, specify the appropriate OData data type by setting
20 // the m:type attribute within the property definition
21 Entity map[string]interface{}
22
23 // When inserting an entity into a table, you must specify values for the PartitionKey and RowKey system properties.
24 // Together, these properties form the primary key and must be unique within the table.
25 // Both the PartitionKey and RowKey values must be string values; each key value may be up to 64 KB in size.
26 // If you are using an integer value for the key value, you should convert the integer to a fixed-width string,
27 // because they are canonically sorted. For example, you should convert the value 1 to 0000001 to ensure proper sorting.
28 RowKey string
29 PartitionKey string
30 }
31
32 // Insert inserts a new entity into a table.
33 func (client Client) Insert(ctx context.Context, accountName, tableName string, input InsertEntityInput) (result autorest.Response, err error) {
34 if accountName == "" {
35 return result, validation.NewError("entities.Client", "Insert", "`accountName` cannot be an empty string.")
36 }
37 if tableName == "" {
38 return result, validation.NewError("entities.Client", "Insert", "`tableName` cannot be an empty string.")
39 }
40 if input.PartitionKey == "" {
41 return result, validation.NewError("entities.Client", "Insert", "`input.PartitionKey` cannot be an empty string.")
42 }
43 if input.RowKey == "" {
44 return result, validation.NewError("entities.Client", "Insert", "`input.RowKey` cannot be an empty string.")
45 }
46
47 req, err := client.InsertPreparer(ctx, accountName, tableName, input)
48 if err != nil {
49 err = autorest.NewErrorWithError(err, "entities.Client", "Insert", nil, "Failure preparing request")
50 return
51 }
52
53 resp, err := client.InsertSender(req)
54 if err != nil {
55 result = autorest.Response{Response: resp}
56 err = autorest.NewErrorWithError(err, "entities.Client", "Insert", resp, "Failure sending request")
57 return
58 }
59
60 result, err = client.InsertResponder(resp)
61 if err != nil {
62 err = autorest.NewErrorWithError(err, "entities.Client", "Insert", resp, "Failure responding to request")
63 return
64 }
65
66 return
67 }
68
69 // InsertPreparer prepares the Insert request.
70 func (client Client) InsertPreparer(ctx context.Context, accountName, tableName string, input InsertEntityInput) (*http.Request, error) {
71 pathParameters := map[string]interface{}{
72 "tableName": autorest.Encode("path", tableName),
73 }
74
75 headers := map[string]interface{}{
76 "x-ms-version": APIVersion,
77 "Accept": fmt.Sprintf("application/json;odata=%s", input.MetaDataLevel),
78 "Prefer": "return-no-content",
79 }
80
81 input.Entity["PartitionKey"] = input.PartitionKey
82 input.Entity["RowKey"] = input.RowKey
83
84 preparer := autorest.CreatePreparer(
85 autorest.AsContentType("application/json"),
86 autorest.AsPost(),
87 autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)),
88 autorest.WithPathParameters("/{tableName}", pathParameters),
89 autorest.WithJSON(input.Entity),
90 autorest.WithHeaders(headers))
91 return preparer.Prepare((&http.Request{}).WithContext(ctx))
92 }
93
94 // InsertSender sends the Insert request. The method will close the
95 // http.Response Body if it receives an error.
96 func (client Client) InsertSender(req *http.Request) (*http.Response, error) {
97 return autorest.SendWithSender(client, req,
98 azure.DoRetryWithRegistration(client.Client))
99 }
100
101 // InsertResponder handles the response to the Insert request. The method always
102 // closes the http.Response Body.
103 func (client Client) InsertResponder(resp *http.Response) (result autorest.Response, err error) {
104 err = autorest.Respond(
105 resp,
106 client.ByInspecting(),
107 azure.WithErrorUnlessStatusCode(http.StatusNoContent),
108 autorest.ByClosing())
109 result = autorest.Response{Response: resp}
110
111 return
112 }