| Conditions | 9 |
| Total Lines | 51 |
| Code Lines | 28 |
| 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:
| 1 | package cmd |
||
| 41 | func runGenerateAst() func(cmd *cobra.Command, args []string) error { |
||
| 42 | return func(cmd *cobra.Command, args []string) error { |
||
| 43 | // Fetch the value of the "pretty" flag. |
||
| 44 | pretty := viper.GetBool("pretty") |
||
| 45 | |||
| 46 | // Parse the provided URL. |
||
| 47 | u, err := url.Parse(args[0]) |
||
| 48 | if err != nil { |
||
| 49 | return err |
||
| 50 | } |
||
| 51 | |||
| 52 | // Initialize a decoder for the provided URL. |
||
| 53 | decoder, err := file.NewDecoderFromURL(u) |
||
| 54 | if err != nil { |
||
| 55 | return err |
||
| 56 | } |
||
| 57 | |||
| 58 | // Initialize an empty shape to store the decoded schema. |
||
| 59 | s := &file.Shape{} |
||
| 60 | |||
| 61 | // Decode the content from the URL into the shape. |
||
| 62 | err = decoder.Decode(s) |
||
| 63 | if err != nil { |
||
| 64 | return err |
||
| 65 | } |
||
| 66 | |||
| 67 | // Convert the string definitions in the shape to a structured schema. |
||
| 68 | def, err := schema.NewSchemaFromStringDefinitions(true, s.Schema) |
||
| 69 | if err != nil { |
||
| 70 | return err |
||
| 71 | } |
||
| 72 | |||
| 73 | // Serialize the schema definition into JSON. |
||
| 74 | jsonData, err := protojson.Marshal(def) |
||
| 75 | if err != nil { |
||
| 76 | return err |
||
| 77 | } |
||
| 78 | |||
| 79 | // Print the JSON, either prettified or raw based on the "pretty" flag. |
||
| 80 | if pretty { |
||
| 81 | var prettyJSON bytes.Buffer |
||
| 82 | err = json.Indent(&prettyJSON, jsonData, "", " ") |
||
| 83 | if err != nil { |
||
| 84 | return err |
||
| 85 | } |
||
| 86 | fmt.Println(prettyJSON.String()) |
||
| 87 | } else { |
||
| 88 | fmt.Println(string(jsonData)) |
||
| 89 | } |
||
| 90 | |||
| 91 | return nil |
||
| 92 | } |
||
| 94 |