Debian Codegraph @

internal/librariangen/request/request.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package request
16
17 import (
18 "encoding/json"
19 "fmt"
20 "os"
21 )
22
23 // Library is the combination of all the fields used by CLI requests and responses.
24 // Each CLI command has its own request/response type, but they all use Library.
25 type Library struct {
26 ID string `json:"id,omitempty"`
27 Version string `json:"version,omitempty"`
28 APIs []API `json:"apis,omitempty"`
29 // SourcePaths are the directories to which librarian contributes code.
30 // For Go, this is typically the Go module directory.
31 SourcePaths []string `json:"source_roots,omitempty"`
32 // PreserveRegex are files/directories to leave untouched during generation.
33 // This is useful for preserving handwritten helper files or customizations.
34 PreserveRegex []string `json:"preserve_regex,omitempty"`
35 // RemoveRegex are files/directories to remove during generation.
36 RemoveRegex []string `json:"remove_regex,omitempty"`
37 // Changes are the changes being released (in a release request)
38 Changes []*Change `json:"changes,omitempty"`
39 // ReleaseTriggered indicates whether this library is being released (in a release request)
40 ReleaseTriggered bool `json:"release_triggered,omitempty"`
41 }
42
43 // API corresponds to a single API definition within a librarian request/response.
44 type API struct {
45 Path string `json:"path,omitempty"`
46 ServiceConfig string `json:"service_config,omitempty"`
47 }
48
49 // Change represents a single commit change for a library.
50 type Change struct {
51 Type string `json:"type"`
52 Subject string `json:"subject"`
53 Body string `json:"body"`
54 PiperCLNumber string `json:"piper_cl_number"`
55 SourceCommitHash string `json:"source_commit_hash"`
56 }
57
58 // ParseLibrary reads a file from the given path and unmarshals
59 // it into a Library struct. This is used for build and generate, where the requests
60 // are simply the library, with no wrapping.
61 func ParseLibrary(path string) (*Library, error) {
62 data, err := os.ReadFile(path)
63 if err != nil {
64 return nil, fmt.Errorf("librariangen: failed to read request file from %s: %w", path, err)
65 }
66
67 var req Library
68 if err := json.Unmarshal(data, &req); err != nil {
69 return nil, fmt.Errorf("librariangen: failed to unmarshal request file %s: %w", path, err)
70 }
71
72 return &req, nil
73 }