Conditions | 18 |
Total Lines | 108 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like servers.*SchemaServer.PartialWrite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package servers |
||
92 | func (r *SchemaServer) PartialWrite(ctx context.Context, request *v1.SchemaPartialWriteRequest) (*v1.SchemaPartialWriteResponse, error) { |
||
93 | // Start a new tracing span for monitoring and observability. |
||
94 | ctx, span := internal.Tracer.Start(ctx, "schemas.partial-write") |
||
95 | defer span.End() // Ensure the span is closed at the end of the function. |
||
96 | |||
97 | // Retrieve or default the schema version from the request. |
||
98 | version := request.GetMetadata().GetSchemaVersion() |
||
99 | if version == "" { // If not provided, fetch the latest version. |
||
100 | ver, err := r.sr.HeadVersion(ctx, request.GetTenantId()) |
||
101 | if err != nil { |
||
102 | return nil, status.Error(GetStatus(err), err.Error()) // Return gRPC status error on failure. |
||
103 | } |
||
104 | version = ver |
||
105 | } |
||
106 | |||
107 | // Fetch the current schema definition as a string. |
||
108 | definitions, err := r.sr.ReadSchemaString(ctx, request.GetTenantId(), version) |
||
109 | if err != nil { |
||
110 | span.RecordError(err) // Log and record the error. |
||
111 | return nil, status.Error(GetStatus(err), err.Error()) |
||
112 | } |
||
113 | |||
114 | // Parse the schema definitions into a structured format. |
||
115 | p := parser.NewParser(strings.Join(definitions, "\n")) |
||
116 | schema, err := p.Parse() |
||
117 | if err != nil { |
||
118 | span.RecordError(err) // Log and record the error. |
||
119 | return nil, status.Error(GetStatus(err), err.Error()) |
||
120 | } |
||
121 | |||
122 | // Iterate through each partial update in the request and apply changes. |
||
123 | for entityName, partials := range request.GetPartials() { |
||
124 | for _, write := range partials.GetWrite() { // Handle new schema statements. |
||
125 | pr := parser.NewParser(write) |
||
126 | stmt, err := pr.ParsePartial(entityName) |
||
127 | if err != nil { |
||
128 | span.RecordError(err) |
||
129 | return nil, status.Error(GetStatus(err), err.Error()) |
||
130 | } |
||
131 | err = schema.AddStatement(entityName, stmt) |
||
132 | if err != nil { |
||
133 | span.RecordError(err) |
||
134 | return nil, status.Error(GetStatus(err), err.Error()) |
||
135 | } |
||
136 | } |
||
137 | |||
138 | for _, update := range partials.GetUpdate() { // Handle schema updates. |
||
139 | pr := parser.NewParser(update) |
||
140 | stmt, err := pr.ParsePartial(entityName) |
||
141 | if err != nil { |
||
142 | span.RecordError(err) |
||
143 | return nil, status.Error(GetStatus(err), err.Error()) |
||
144 | } |
||
145 | err = schema.UpdateStatement(entityName, stmt) |
||
146 | if err != nil { |
||
147 | span.RecordError(err) |
||
148 | return nil, status.Error(GetStatus(err), err.Error()) |
||
149 | } |
||
150 | } |
||
151 | |||
152 | for _, del := range partials.GetDelete() { // Handle schema deletions. |
||
153 | err = schema.DeleteStatement(entityName, del) |
||
154 | if err != nil { |
||
155 | span.RecordError(err) |
||
156 | return nil, status.Error(GetStatus(err), err.Error()) |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Re-parse the updated schema to ensure consistency. |
||
162 | sch, err := parser.NewParser(schema.String()).Parse() |
||
163 | if err != nil { |
||
164 | span.RecordError(err) |
||
165 | return nil, status.Error(GetStatus(err), err.Error()) |
||
166 | } |
||
167 | |||
168 | // Compile the new schema to validate its correctness. |
||
169 | _, _, err = compiler.NewCompiler(true, sch).Compile() |
||
170 | if err != nil { |
||
171 | span.RecordError(err) |
||
172 | return nil, status.Error(GetStatus(err), err.Error()) |
||
173 | } |
||
174 | |||
175 | // Generate a new version ID for the updated schema. |
||
176 | newVersion := xid.New().String() |
||
177 | |||
178 | // Prepare the new schema definition for storage. |
||
179 | cnf := make([]storage.SchemaDefinition, 0, len(sch.Statements)) |
||
180 | for _, st := range sch.Statements { |
||
181 | cnf = append(cnf, storage.SchemaDefinition{ |
||
182 | TenantID: request.GetTenantId(), |
||
183 | Version: newVersion, |
||
184 | Name: st.GetName(), |
||
185 | SerializedDefinition: []byte(st.String()), |
||
186 | }) |
||
187 | } |
||
188 | |||
189 | // Write the updated schema to storage. |
||
190 | err = r.sw.WriteSchema(ctx, cnf) |
||
191 | if err != nil { |
||
192 | span.RecordError(err) |
||
193 | return nil, status.Error(GetStatus(err), err.Error()) |
||
194 | } |
||
195 | |||
196 | // Return the response with the new schema version. |
||
197 | return &v1.SchemaPartialWriteResponse{ |
||
198 | SchemaVersion: newVersion, |
||
199 | }, nil |
||
200 | } |
||
257 |