{{!
  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

    NOTE:
    Unfortunately map literals cannot be used to store metadata, due to a bug
    in the Go compiler: https://github.com/golang/go/issues/33437
    The bug results in a "NewBulk too big" compilation error for some very large schemas.
    The workaround is to create slice literals (one with keys, one with values),
    create an empty map during runtime and populate that map from the two slices.

}}{{> common/header}}

import (
    "reflect"
    "sync"

    {{#program:thrift_imports}}
    {{program:go_package_alias}} "{{program:go_import_path}}"
    {{/program:thrift_imports}}
    thrift "{{program:thrift_lib_import}}"
)

{{> common/unused_imports_protection}}
// (needed to ensure safety because of naive import list construction)
var _ = thrift.ZERO
var _ = reflect.Ptr

// Premade codec specs
var (
    {{#program:thrift_metadata_types}}
    {{type:codec_type_spec_name}} *thrift.TypeSpec = nil
    {{/program:thrift_metadata_types}}
)

// Premade codec specs initializer
var premadeCodecSpecsInitOnce = sync.OnceFunc(func() {
    {{#program:thrift_metadata_types}}
    {{type:codec_type_spec_name}} = &thrift.TypeSpec{
        {{> spec/codec_type_spec_setter}}: {{> spec/codec_type_spec_instance}}
    }
    {{/program:thrift_metadata_types}}
})

// Premade struct specs
var (
    {{#program:structs}}
    {{struct:struct_spec_name}} *thrift.StructSpec = nil
    {{/program:structs}}
    {{#program:services}}
    {{#service:req_resp_structs}}
    {{struct:struct_spec_name}} *thrift.StructSpec = nil
    {{/service:req_resp_structs}}
    {{/program:services}}
)

// Premade struct specs initializer
var premadeStructSpecsInitOnce = sync.OnceFunc(func() {
    {{#program:structs}}
    {{struct:struct_spec_name}} = {{> spec/codec_struct_spec}}
    {{/program:structs}}
    {{#program:services}}
    {{#service:req_resp_structs}}
    {{struct:struct_spec_name}} = {{> spec/codec_struct_spec}}
    {{/service:req_resp_structs}}
    {{/program:services}}
})

// Helper type to allow us to store codec specs in a slice at compile time,
// and put them in a map at runtime. See comment at the top of template
// about a compilation limitation that affects map literals.
type codecSpecWithFullName struct {
    fullName string
    typeSpec *thrift.TypeSpec
}

var premadeCodecSpecsMapOnce = sync.OnceValue(
    func() map[string]*thrift.TypeSpec {
        // Relies on premade codec specs initialization
        premadeCodecSpecsInitOnce()

        codecSpecsWithFullName := make([]codecSpecWithFullName, 0)
        {{#program:thrift_metadata_types}}
        {{#type:named?}}
        codecSpecsWithFullName = append(codecSpecsWithFullName, codecSpecWithFullName{ "{{type:full_name}}", {{type:codec_type_spec_name}} })
        {{/type:named?}}
        {{/program:thrift_metadata_types}}

        fbthriftTypeSpecsMap := make(map[string]*thrift.TypeSpec, len(codecSpecsWithFullName))
        for _, value := range codecSpecsWithFullName {
            fbthriftTypeSpecsMap[value.fullName] = value.typeSpec
        }
        return fbthriftTypeSpecsMap
    },
)

func init() {
    premadeCodecSpecsInitOnce()
    premadeStructSpecsInitOnce()
}

// GetMetadataThriftType (INTERNAL USE ONLY).
// Returns metadata TypeSpec for a given full type name.
func GetCodecTypeSpec(fullName string) *thrift.TypeSpec {
    return premadeCodecSpecsMapOnce()[fullName]
}
