| 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 |
||
| 81 | func (r *SchemaServer) PartialWrite(ctx context.Context, request *v1.SchemaPartialWriteRequest) (*v1.SchemaPartialWriteResponse, error) { |
||
| 82 | // Start a new tracing span for monitoring and observability. |
||
| 83 | ctx, span := tracer.Start(ctx, "schemas.partial-write") |
||
| 84 | defer span.End() // Ensure the span is closed at the end of the function. |
||
| 85 | |||
| 86 | // Retrieve or default the schema version from the request. |
||
| 87 | version := request.GetMetadata().GetSchemaVersion() |
||
| 88 | if version == "" { // If not provided, fetch the latest version. |
||
| 89 | ver, err := r.sr.HeadVersion(ctx, request.GetTenantId()) |
||
| 90 | if err != nil { |
||
| 91 | return nil, status.Error(GetStatus(err), err.Error()) // Return gRPC status error on failure. |
||
| 92 | } |
||
| 93 | version = ver |
||
| 94 | } |
||
| 95 | |||
| 96 | // Fetch the current schema definition as a string. |
||
| 97 | definitions, err := r.sr.ReadSchemaString(ctx, request.GetTenantId(), version) |
||
| 98 | if err != nil { |
||
| 99 | span.RecordError(err) // Log and record the error. |
||
| 100 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 101 | } |
||
| 102 | |||
| 103 | // Parse the schema definitions into a structured format. |
||
| 104 | p := parser.NewParser(strings.Join(definitions, "\n")) |
||
| 105 | schema, err := p.Parse() |
||
| 106 | if err != nil { |
||
| 107 | span.RecordError(err) // Log and record the error. |
||
| 108 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 109 | } |
||
| 110 | |||
| 111 | // Iterate through each partial update in the request and apply changes. |
||
| 112 | for entityName, partials := range request.GetPartials() { |
||
| 113 | for _, write := range partials.GetWrite() { // Handle new schema statements. |
||
| 114 | pr := parser.NewParser(write) |
||
| 115 | stmt, err := pr.ParsePartial(entityName) |
||
| 116 | if err != nil { |
||
| 117 | span.RecordError(err) |
||
| 118 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 119 | } |
||
| 120 | err = schema.AddStatement(entityName, stmt) |
||
| 121 | if err != nil { |
||
| 122 | span.RecordError(err) |
||
| 123 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | for _, update := range partials.GetUpdate() { // Handle schema updates. |
||
| 128 | pr := parser.NewParser(update) |
||
| 129 | stmt, err := pr.ParsePartial(entityName) |
||
| 130 | if err != nil { |
||
| 131 | span.RecordError(err) |
||
| 132 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 133 | } |
||
| 134 | err = schema.UpdateStatement(entityName, stmt) |
||
| 135 | if err != nil { |
||
| 136 | span.RecordError(err) |
||
| 137 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | for _, del := range partials.GetDelete() { // Handle schema deletions. |
||
| 142 | err = schema.DeleteStatement(entityName, del) |
||
| 143 | if err != nil { |
||
| 144 | span.RecordError(err) |
||
| 145 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Re-parse the updated schema to ensure consistency. |
||
| 151 | sch, err := parser.NewParser(schema.String()).Parse() |
||
| 152 | if err != nil { |
||
| 153 | span.RecordError(err) |
||
| 154 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 155 | } |
||
| 156 | |||
| 157 | // Compile the new schema to validate its correctness. |
||
| 158 | _, _, err = compiler.NewCompiler(true, sch).Compile() |
||
| 159 | if err != nil { |
||
| 160 | span.RecordError(err) |
||
| 161 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 162 | } |
||
| 163 | |||
| 164 | // Generate a new version ID for the updated schema. |
||
| 165 | newVersion := xid.New().String() |
||
| 166 | |||
| 167 | // Prepare the new schema definition for storage. |
||
| 168 | cnf := make([]storage.SchemaDefinition, 0, len(sch.Statements)) |
||
| 169 | for _, st := range sch.Statements { |
||
| 170 | cnf = append(cnf, storage.SchemaDefinition{ |
||
| 171 | TenantID: request.GetTenantId(), |
||
| 172 | Version: newVersion, |
||
| 173 | Name: st.GetName(), |
||
| 174 | SerializedDefinition: []byte(st.String()), |
||
| 175 | }) |
||
| 176 | } |
||
| 177 | |||
| 178 | // Write the updated schema to storage. |
||
| 179 | err = r.sw.WriteSchema(ctx, cnf) |
||
| 180 | if err != nil { |
||
| 181 | span.RecordError(err) |
||
| 182 | return nil, status.Error(GetStatus(err), err.Error()) |
||
| 183 | } |
||
| 184 | |||
| 185 | // Return the response with the new schema version. |
||
| 186 | return &v1.SchemaPartialWriteResponse{ |
||
| 187 | SchemaVersion: newVersion, |
||
| 188 | }, nil |
||
| 189 | } |
||
| 242 |