go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/api/projects/v1/config.go (about)

     1  // Copyright 2019 The LUCI Authors.
     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 projects
    16  
    17  import (
    18  	"google.golang.org/protobuf/proto"
    19  
    20  	"go.chromium.org/luci/config/validation"
    21  	"go.chromium.org/luci/gae/service/datastore"
    22  )
    23  
    24  // Ensure Config implements datastore.PropertyConverter.
    25  // This allows projects to be read from and written to the datastore.
    26  var _ datastore.PropertyConverter = &Config{}
    27  
    28  // FromProperty implements datastore.PropertyConverter.
    29  func (cfg *Config) FromProperty(p datastore.Property) error {
    30  	if p.Value() == nil {
    31  		cfg = &Config{}
    32  		return nil
    33  	}
    34  	return proto.Unmarshal(p.Value().([]byte), cfg)
    35  }
    36  
    37  // ToProperty implements datastore.PropertyConverter.
    38  func (cfg *Config) ToProperty() (datastore.Property, error) {
    39  	p := datastore.Property{}
    40  	bytes, err := proto.Marshal(cfg)
    41  	if err != nil {
    42  		return datastore.Property{}, err
    43  	}
    44  	// noindex is not respected in the tags in the model.
    45  	return p, p.SetValue(bytes, datastore.NoIndex)
    46  }
    47  
    48  // Validate validates this config.
    49  func (cfg *Config) Validate(c *validation.Context) {
    50  	if cfg.GetProject() == "" {
    51  		c.Errorf("project is required")
    52  	}
    53  	if cfg.GetRevision() != "" {
    54  		c.Errorf("revision must not be specified")
    55  	}
    56  }