|
| 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 | +// https://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 discovery |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/googleapis/librarian/internal/sidekick/internal/api" |
| 21 | +) |
| 22 | + |
| 23 | +func maybeMapField(model *api.API, message *api.Message, input *property) (*api.Field, error) { |
| 24 | + if input.Schema.Type != "object" || input.Schema.Format != "" { |
| 25 | + return nil, nil |
| 26 | + } |
| 27 | + if input.Schema.AdditionalProperties == nil { |
| 28 | + return nil, nil |
| 29 | + } |
| 30 | + |
| 31 | + if field := maybeMapOfObjectField(model, message, input); field != nil { |
| 32 | + return field, nil |
| 33 | + } |
| 34 | + if field, err := maybeMapOfEnumField(model, message, input); err != nil || field != nil { |
| 35 | + return field, err |
| 36 | + } |
| 37 | + return maybeMapOfPrimitiveField(model, message, input) |
| 38 | +} |
| 39 | + |
| 40 | +func maybeMapOfObjectField(model *api.API, message *api.Message, input *property) *api.Field { |
| 41 | + if input.Schema.AdditionalProperties.Ref == "" { |
| 42 | + return nil |
| 43 | + } |
| 44 | + valueTypezID := fmt.Sprintf(".%s.%s", model.PackageName, input.Schema.AdditionalProperties.Ref) |
| 45 | + typezID := insertMapType(model, api.MESSAGE_TYPE, valueTypezID) |
| 46 | + field := &api.Field{ |
| 47 | + Name: input.Name, |
| 48 | + JSONName: input.Name, |
| 49 | + ID: fmt.Sprintf("%s.%s", message.ID, input.Name), |
| 50 | + Documentation: input.Schema.Description, |
| 51 | + Typez: api.MESSAGE_TYPE, |
| 52 | + TypezID: typezID, |
| 53 | + } |
| 54 | + return field |
| 55 | +} |
| 56 | + |
| 57 | +func maybeMapOfEnumField(model *api.API, message *api.Message, input *property) (*api.Field, error) { |
| 58 | + if input.Schema.AdditionalProperties.Enums == nil { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + if err := makeMessageEnum(model, message, input.Name, input.Schema.AdditionalProperties); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + valueTypezID := fmt.Sprintf("%s.%s", message.ID, input.Name) |
| 65 | + typezID := insertMapType(model, api.ENUM_TYPE, valueTypezID) |
| 66 | + field := &api.Field{ |
| 67 | + Name: input.Name, |
| 68 | + JSONName: input.Name, |
| 69 | + ID: fmt.Sprintf("%s.%s", message.ID, input.Name), |
| 70 | + Documentation: input.Schema.Description, |
| 71 | + Typez: api.MESSAGE_TYPE, |
| 72 | + TypezID: typezID, |
| 73 | + } |
| 74 | + return field, nil |
| 75 | +} |
| 76 | + |
| 77 | +func maybeMapOfPrimitiveField(model *api.API, message *api.Message, input *property) (*api.Field, error) { |
| 78 | + valueTypez, valueTypezID, err := scalarType(model, message.ID, input.Name, input.Schema.AdditionalProperties) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + typezID := insertMapType(model, valueTypez, valueTypezID) |
| 83 | + field := &api.Field{ |
| 84 | + Name: input.Name, |
| 85 | + JSONName: input.Name, |
| 86 | + ID: fmt.Sprintf("%s.%s", message.ID, input.Name), |
| 87 | + Documentation: input.Schema.Description, |
| 88 | + Typez: api.MESSAGE_TYPE, |
| 89 | + TypezID: typezID, |
| 90 | + } |
| 91 | + return field, nil |
| 92 | +} |
| 93 | + |
| 94 | +func insertMapType(model *api.API, valueTypez api.Typez, valueTypezId string) string { |
| 95 | + id := fmt.Sprintf("$map<string, %s>", valueTypezId) |
| 96 | + if _, ok := model.State.MessageByID[id]; ok { |
| 97 | + return id |
| 98 | + } |
| 99 | + key := &api.Field{ |
| 100 | + Name: "key", |
| 101 | + ID: fmt.Sprintf("%s.key", id), |
| 102 | + Typez: api.STRING_TYPE, |
| 103 | + TypezID: "string", |
| 104 | + } |
| 105 | + value := &api.Field{ |
| 106 | + Name: "value", |
| 107 | + ID: fmt.Sprintf("%s.value", id), |
| 108 | + Typez: valueTypez, |
| 109 | + TypezID: valueTypezId, |
| 110 | + } |
| 111 | + message := &api.Message{ |
| 112 | + Name: id, |
| 113 | + ID: id, |
| 114 | + Documentation: id, |
| 115 | + Package: "$", |
| 116 | + IsMap: true, |
| 117 | + Fields: []*api.Field{key, value}, |
| 118 | + } |
| 119 | + model.State.MessageByID[message.ID] = message |
| 120 | + return id |
| 121 | +} |
0 commit comments